Instantiating a Derived from a Base

Discussion in 'C++' started by Randy, May 14, 2007.

  1. Randy

    Randy Guest

    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;
    }
     
    Randy, May 14, 2007
    #1
    1. Advertising

  2. Randy

    Salt_Peter Guest

    On May 13, 10:36 pm, Randy <> 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;
    >
    > }
     
    Salt_Peter, May 14, 2007
    #2
    1. Advertising

  3. Randy

    Randy Guest

    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.
     
    Randy, May 14, 2007
    #3
  4. Randy

    Old Wolf Guest

    On May 15, 8:09 am, Randy <> 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.
     
    Old Wolf, May 15, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    577
    qazmlp
    Apr 10, 2005
  2. Alex Martelli
    Replies:
    3
    Views:
    302
    Achim Dahlhoff
    Nov 29, 2005
  3. Replies:
    4
    Views:
    426
    Alf P. Steinbach
    May 23, 2007
  4. Replies:
    1
    Views:
    403
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    393
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page