Instantiating a Derived from a Base

R

Randy

Hi,

I was learning about RTTI when I ran across this example. This line,
out of the example, confused me. It is declaring a pointer to a base
type and instantiating it with a derived class. I can say the
words ... yet I don't get it. What do I have, a base or a derived? Can
anyone push me in the right direction.

abc *abc_pointer = new xyz();


/*
-------------------------------------------------------------------------------------------------
*/

#include <iostream>

class abc // base class
{
public:
virtual void hello()
{
std::cout << "in abc";
}
};

class xyz : public abc
{
public:
void hello()
{
std::cout << "in xyz";
}
};

int main()
{
abc *abc_pointer = new xyz();
xyz *xyz_pointer;

// to find whether abc is pointing to xyz type of object
xyz_pointer = dynamic_cast<xyz*>(abc_pointer);

if (xyz_pointer != NULL)
std::cout << "abc pointer is pointing to a xyz class object"; //
identified
else
std::cout << "abc pointer is NOT pointing to a xyz class object";

return 0;
}
 
S

Salt_Peter

Hi,

I was learning about RTTI when I ran across this example. This line,
out of the example, confused me. It is declaring a pointer to a base
type and instantiating it with a derived class. I can say the
words ... yet I don't get it. What do I have, a base or a derived? Can
anyone push me in the right direction.

abc *abc_pointer = new xyz();

Look at it this way, all derived objects are really base objects that
have been repackaged or specialized. That means you can point to
either the whole object or the underlying base package.
A derived object does not have a base object in it. A derived object
is_a base object.

In the above code you have a derived object but the pointer holds that
base's address.
If you have the base's address you can access the derived object if
you need to.
Read about downcasting and upcasting in C++.

struct Bird { ... };
struct Eagle : public Bird { ... };
struct Pigeon : public Bird { ... };

int main()
{
Eagle eagle; // a special type of Bird
Pigeon pigeon; // another special type of Bird
Bird* p_bird = &eagle; // base ptr points to Eagle
p_bird = &pigeon; // ok, base ptr changed to point to Pigeon

Eagle* p_eagle = &eagle; // ok, no problem
p_eagle = &pigeon; // error !!!
}
 
R

Randy

abc *abc_pointer = new xyz();

@Salt_Peter Thank you for your time and examples. I get it now after
some reading and practice. I read up on up/down casting as well.

Base and derived are the same object. In the example, I have created a
new instance of a derived class but the pointer I created to it points
to it's base. I am pointing to a different "schema" of the same
object. I tested and, as expected, I couldn't access the derived
object's methods ... just the base methods. I had to downcast before I
could call the derived objects methods. ... all within the same
object.
 
O

Old Wolf

abc *abc_pointer = new xyz();

Base and derived are the same object. In the example, I have created a
new instance of a derived class but the pointer I created to it points
to it's base. I am pointing to a different "schema" of the same
object. I tested and, as expected, I couldn't access the derived
object's methods ... just the base methods. I had to downcast before I
could call the derived objects methods. ... all within the same
object.

That's right. Note that your code has a memory leak because
you never delete the object, and even if you did write:
delete abc_pointer;

it would cause undefined behaviour because the class
does not have a virtual destructor.

If you plan on deleting objects through a pointer to their
base class then the base class needs to have a virtual
destructor. This is so that 'delete' knows that it is not
just destructing a Base object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top