Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Arrays of derived objects - newbie

Reply
Thread Tools

Arrays of derived objects - newbie

 
 
Robert
Guest
Posts: n/a
 
      01-05-2007
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

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-05-2007

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).

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      01-05-2007

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.

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007
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++.

 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-12-2007
On Jan 12, 5:32 am, "Grizlyk" <(E-Mail Removed)> 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

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-14-2007

Erik Wikström wrote:

> On Jan 12, 5:32 am, "Grizlyk" <(E-Mail Removed)> 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.

 
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
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
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
Arrays of derived objects Jack C++ 25 03-22-2006 07:22 PM



Advertisments