Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > downcasting in c++ I couldnt success

Reply
Thread Tools

downcasting in c++ I couldnt success

 
 
eMRe
Guest
Posts: n/a
 
      10-05-2008
class Animal {


};

class Dog : public Animal {

};

int main ( )
{
stack<Animal> mystack;
Animal *x = new Dog( );
mystack.push(*x);


Animal y = mystack.top( );

Dog z = ( Dog ) y ; // this line doesnt work, how could I fix it?

return 0;
}
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-05-2008
eMRe wrote:

> class Animal {
>
>
> };
>
> class Dog : public Animal {
>
> };
>
> int main ( )
> {
> stack<Animal> mystack;
> Animal *x = new Dog( );
> mystack.push(*x);


Containers in C++ have value semantics, i.e., the line above only copies the
Animal part of the Dog object. What the stack contains is an honest to God
Animal and no Dog.

>
> Animal y = mystack.top( );


That way, you retrieve the Animal you stored.

> Dog z = ( Dog ) y ; // this line doesnt work, how could I fix it?


You should get rid of the C-style cast. But anyway, the line above should
not succeed. The Dog part of the object has been lost when you stored it in
the stack.

> return 0;
> }



If you need a polymorphic stack, you might try

stack< Animal * >


Technically, if D is derived from T, there are two natural maps:

a) a projection from values of type D to values of type T. This map is
called "slicing".

b) an injection from values of type D* to values of type T*. This map is
what people usually call the is-a-relation. It is important to see that it
holds on the level of pointers (or references, but that does not matter in
the context of containers).


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-06-2008
On Oct 5, 9:36 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> eMRe wrote:
> > class Animal {
> > };


> > class Dog : public Animal {
> > };


> > int main ( )
> > {
> > stack<Animal> mystack;
> > Animal *x = new Dog( );
> > mystack.push(*x);


> Containers in C++ have value semantics, i.e., the line above
> only copies the Animal part of the Dog object. What the stack
> contains is an honest to God Animal and no Dog.


> > Animal y = mystack.top( );


> That way, you retrieve the Animal you stored.


He retrieves a copy of the Animal he stored. Not the Animal
itself. Even if mystack had type stack<Dog>, all he'd get is a
copy of the Animal part of the Dog in the stack, because his
variable is of type Animal.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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
downcasting in c++ I couldnt success eMRe C Programming 15 10-07-2008 06:41 AM
I couldnt find what is wrong emre esirik(hacettepe computer science and engineering) C Programming 2 11-15-2007 11:01 PM
easy but couldnt see the error jw C++ 5 11-11-2005 08:48 PM
couldnt detect DVD drive yuwandib@gmail.com Windows 64bit 0 09-05-2005 02:48 AM
Re: Server couldnt notice object that i appended (ClientSide) ... Karl Seguin ASP .Net 1 07-28-2003 12:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57