Arrays of derived objects - newbie

R

Robert

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
 
O

Ondra Holub

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

Noah Roberts

Robert said:
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.
 
G

Grizlyk

Robert said:
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++.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Robert said:
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?
 
G

Grizlyk

Erik said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top