Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Instantiating a Derived from a Base

Reply
Thread Tools

Instantiating a Derived from a Base

 
 
Randy
Guest
Posts: n/a
 
      05-14-2007
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;
}

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      05-14-2007
On May 13, 10:36 pm, Randy <(E-Mail Removed)> wrote:
> 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 !!!
}

>
> /*
> -------------------------------------------------------------------------------------------------
> */
>
> #include <iostream>
>
> class abc // base class
> {
> public:
> virtual void hello()
> {
> std::cout << "in abc";
> }
>
> };Do a search for upcasting and downcasting
>
> 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;
>
> }



 
Reply With Quote
 
 
 
 
Randy
Guest
Posts: n/a
 
      05-14-2007
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.



 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-14-2007
On May 15, 8:09 am, Randy <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from base to derived class in base constructor pastbin@gmail.com C++ 2 02-07-2008 02:41 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Format of compiler generated derived destructor when base has 'virtual ~base() throw():" qazmlp C++ 1 04-10-2005 03:09 PM



Advertisments