upcasting in vector

M

majsta

Hello, I have the following code.

#include <vector>
#include <iostream>

class Foo {
public:
Foo(){}
virtual void print() const { std::cout << "foo" << std::endl;}
};

class Boo:public Foo {
public:
Boo():Foo(){}
virtual void print() const { std::cout << "boo" << std::endl;}
};

int main(void) {
std::vector< Foo > vec;
Foo ff;
Boo bb;
vec.push_back(ff);
vec.push_back(bb);
std::vector< Foo >::iterator Ivec = vec.begin();
while(Ivec != vec.end()){
(*Ivec++).print();
}
std::cout << "===========" << std::endl;
std::vector< Foo* > vec2;
vec2.push_back(&ff);
vec2.push_back(&bb);
std::vector< Foo* >::iterator Ivec2 = vec2.begin();
while(Ivec2 != vec2.end()){
(*Ivec2++)->print();
}
return 0;
};


the output is:

foo
foo
===========
foo
boo

so when I create a vector of pointers to Foo, the upcasting of print()
works fine, but it does not for a vector of Foo.

Could you, please, tell me what am I missing here?

Thank you very much for help and your time

m
 
D

diligent.snail

vec.push_back(bb);

This makes a copy of the Foo subobject in bb. Polymorphic behaviour is
obtained through pointers or references to base objects.
 
J

Jim Langston

Hello, I have the following code.

#include <vector>
#include <iostream>

class Foo {
public:
Foo(){}
virtual void print() const { std::cout << "foo" << std::endl;}
};

class Boo:public Foo {
public:
Boo():Foo(){}
virtual void print() const { std::cout << "boo" << std::endl;}
};

int main(void) {
std::vector< Foo > vec;
Foo ff;
Boo bb;
vec.push_back(ff);
vec.push_back(bb);
std::vector< Foo >::iterator Ivec = vec.begin();
while(Ivec != vec.end()){
(*Ivec++).print();
}
std::cout << "===========" << std::endl;
std::vector< Foo* > vec2;
vec2.push_back(&ff);
vec2.push_back(&bb);
std::vector< Foo* >::iterator Ivec2 = vec2.begin();
while(Ivec2 != vec2.end()){
(*Ivec2++)->print();
}
return 0;
};


the output is:

foo
foo
===========
foo
boo

so when I create a vector of pointers to Foo, the upcasting of print()
works fine, but it does not for a vector of Foo.

Could you, please, tell me what am I missing here?

Thank you very much for help and your time

Slicing. In your vec you are storing Foo. When you push_back it is dony by
copy, so your Boo is copied to a Foo. Only the Foo part of Boo is taken.
So your vec contains instances of Foo with none of the Bar part.

Polymorphism is done by pointers. In your vec2 you are pushing pointers,
the pointer values are copied, the isntances of Foo and Boo are left alone,
they never get copied or changed.
 
R

Ron Natalie

so when I create a vector of pointers to Foo, the upcasting of print()
works fine, but it does not for a vector of Foo.
Let me make it simpler, without vectors.

Boo b;
Foo f;

f = b;


f is of type "Foo" not "Boo", the Foo part of the Boo b has
been "sliced" and assigned into f.

f is and will remain of type foo regardless of whether it is
initialized or assigned from a Boo object.

vector<Foo> is the same way. You are copying Foo parts of the
Boo object to Foo objects when you insert them into this vector.
Vector does all of it's insertion (and internal maintenance) with
combinations of the copy-assignment operator and copy constructor
(a requirement of any object you wish to store within is that these
functions can faithfully copy the object).
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top