Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Instantiating a Derived from a Base (http://www.velocityreviews.com/forums/t506265-instantiating-a-derived-from-a-base.html)

Randy 05-14-2007 02:36 AM

Instantiating a Derived from a Base
 
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;
}


Salt_Peter 05-14-2007 03:37 AM

Re: Instantiating a Derived from a Base
 
On May 13, 10:36 pm, Randy <gast...@sympatico.ca> 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;
>
> }




Randy 05-14-2007 08:09 PM

Re: Instantiating a Derived from a Base
 
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.




Old Wolf 05-14-2007 11:00 PM

Re: Instantiating a Derived from a Base
 
On May 15, 8:09 am, Randy <gast...@sympatico.ca> 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.



All times are GMT. The time now is 04:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.