Arrays of derived objects - newbie

Discussion in 'C++' started by Robert, Jan 5, 2007.

  1. Robert

    Robert Guest

    In the code which follows, could someone let me know why the second
    call to Print() calls the base class Print() rather than the derived
    class Print()? I've started going through the C++ FAQs but haven't
    found the answer to this one yet (though I have found that I should be
    using standard library containers rather than arrays [FAQ 34.1]).

    TBase.h:

    #include <iostream>

    class TBase
    {
    public:
    TBase(void) {};
    ~TBase(void) {};
    virtual void Print() const = 0 {std::cout << "Print from TBase" <<
    std::endl; }
    };

    TDerived_A.h:

    #include "TBase.h"
    #include <iostream>

    class TDerived_A : public TBase
    {
    public:
    TDerived_A(void) {};
    virtual void Print() const { std::cout << "Print from TDerived_A" <<
    std::endl; }
    ~TDerived_A(void) {};
    };

    test.cpp:

    #include "TBase.h"
    #include "TDerived_A.h"
    #include <iostream>

    int main( void )
    {
    TDerived_A derivedA;

    TBase *arrTBase_1[] = {&derivedA};
    arrTBase_1[0]->Print();

    TBase arrTBase_2[] = {TDerived_A()};
    arrTBase_2[0].Print();
    }

    output:

    Print from TDerived_A
    Print from TBase
    Robert, Jan 5, 2007
    #1
    1. Advertising

  2. Robert

    Ondra Holub Guest

    Robert napsal:
    > In the code which follows, could someone let me know why the second
    > call to Print() calls the base class Print() rather than the derived
    > class Print()? I've started going through the C++ FAQs but haven't
    > found the answer to this one yet (though I have found that I should be
    > using standard library containers rather than arrays [FAQ 34.1]).
    >
    > TBase.h:
    >
    > #include <iostream>
    >
    > class TBase
    > {
    > public:
    > TBase(void) {};
    > ~TBase(void) {};
    > virtual void Print() const = 0 {std::cout << "Print from TBase" <<
    > std::endl; }
    > };
    >
    > TDerived_A.h:
    >
    > #include "TBase.h"
    > #include <iostream>
    >
    > class TDerived_A : public TBase
    > {
    > public:
    > TDerived_A(void) {};
    > virtual void Print() const { std::cout << "Print from TDerived_A" <<
    > std::endl; }
    > ~TDerived_A(void) {};
    > };
    >
    > test.cpp:
    >
    > #include "TBase.h"
    > #include "TDerived_A.h"
    > #include <iostream>
    >
    > int main( void )
    > {
    > TDerived_A derivedA;
    >
    > TBase *arrTBase_1[] = {&derivedA};
    > arrTBase_1[0]->Print();
    >
    > TBase arrTBase_2[] = {TDerived_A()};
    > arrTBase_2[0].Print();
    > }
    >
    > output:
    >
    > Print from TDerived_A
    > Print from TBase


    For polymorphism you need either pointer to instance or reference to
    instance.

    TBase arrTBase_2[] = {TDerived_A()}; initializes instance of TBase with
    instance of TDerived_A. So instance of TDerived_A is casted to it's
    parent type TBase and arrTBase_2[0] is initialized with it (with copy
    constructor).
    Ondra Holub, Jan 5, 2007
    #2
    1. Advertising

  3. Robert

    Noah Roberts Guest

    Robert wrote:
    > In the code which follows, could someone let me know why the second
    > call to Print() calls the base class Print() rather than the derived
    > class Print()?


    > int main( void )
    > {
    > TDerived_A derivedA;
    >
    > TBase *arrTBase_1[] = {&derivedA};
    > arrTBase_1[0]->Print();
    >
    > TBase arrTBase_2[] = {TDerived_A()};


    You've "sliced" the newly constructed TDerived_A; created a new TBase
    out of it (uses default copy constructor for TBase). The temporary
    created with TDerived_A() is then discarded.

    > arrTBase_2[0].Print();


    This will of course print that a TBase is being called because that is
    what you have.

    > output:
    >
    > Print from TDerived_A
    > Print from TBase


    As expected.
    Noah Roberts, Jan 5, 2007
    #3
  4. Robert

    Grizlyk Guest

    Robert wrote:

    > I've started going through the C++ FAQs but haven't
    > found the answer to this one yet (though I have found that I should be
    > using standard library containers rather than arrays [FAQ 34.1]).


    If you do not intend to learn OO desing of classes at this stage, try
    learn C/C++ memory internals - what is:
    1. stack, heap, defined and undefined static data
    2. function's parameter, styles of transfer of parameters to function
    3. class or structure in memory, derived class in memory

    This is C problem rather than C++.
    Grizlyk, Jan 12, 2007
    #4
  5. On Jan 12, 5:32 am, "Grizlyk" <> wrote:
    > Robert wrote:
    > > I've started going through the C++ FAQs but haven't
    > > found the answer to this one yet (though I have found that I should be
    > > using standard library containers rather than arrays [FAQ 34.1]).If you do not intend to learn OO desing of classes at this stage, try

    > learn C/C++ memory internals - what is:
    > 1. stack, heap, defined and undefined static data
    > 2. function's parameter, styles of transfer of parameters to function
    > 3. class or structure in memory, derived class in memory
    >
    > This is C problem rather than C++.


    Out of curiosity, is it using the standard library containers that is a
    C problem or the slicing of classes?

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 12, 2007
    #5
  6. Robert

    Grizlyk Guest

    Erik Wikström wrote:

    > On Jan 12, 5:32 am, "Grizlyk" <> wrote:
    > > This is C problem rather than C++.

    >
    > Out of curiosity, is it using the standard library containers that is a
    > C problem or the slicing of classes?


    I want to say, C++ do not introduce to the trouble no one new property,
    that can not be resolved by C-language facilities.

    I think, if you can imagine class's data allocated in memory and style
    of parameter passing or object usage, you can easy guess the "lost of
    derived data" trouble without great difficulties.
    Grizlyk, Jan 14, 2007
    #6
    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. Jack
    Replies:
    25
    Views:
    661
    Daniel T.
    Mar 22, 2006
  2. Replies:
    4
    Views:
    389
    Alf P. Steinbach
    May 23, 2007
  3. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
  5. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
Loading...

Share This Page