Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Arrays of derived objects - newbie (http://www.velocityreviews.com/forums/t459656-arrays-of-derived-objects-newbie.html)

Robert 01-05-2007 12:50 PM

Arrays of derived objects - newbie
 
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


Ondra Holub 01-05-2007 01:31 PM

Re: Arrays of derived objects - newbie
 

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


Noah Roberts 01-05-2007 05:06 PM

Re: Arrays of derived objects - newbie
 

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.


Grizlyk 01-12-2007 04:32 AM

Re: Arrays of derived objects - newbie
 
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++.


=?iso-8859-1?q?Erik_Wikstr=F6m?= 01-12-2007 07:28 AM

Re: Arrays of derived objects - newbie
 
On Jan 12, 5:32 am, "Grizlyk" <grizl...@yandex.ru> 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


Grizlyk 01-14-2007 12:13 PM

Re: Arrays of derived objects - newbie
 

Erik Wikström wrote:

> On Jan 12, 5:32 am, "Grizlyk" <grizl...@yandex.ru> 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.



All times are GMT. The time now is 05:51 AM.

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