James Kanze
10/6/2008 9:29:00 AM
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:james.kanze@gmail.com
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