Connecting Tech Pros Worldwide Help | Site Map

HELP: vector & polymorphism

The Directive
Guest
 
Posts: n/a
#1: Jul 22 '05
I can't understand why in this folling example the message
"Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because
of polymorphism? How can I rewrite this example using a vector to
achieve polymorphism. Help!!! I'm very confused.

//Base class.
class Base
{
public:
Base()
{}

virtual void Draw()
{
cout << "Base.Draw()\n";
}
};

//Derive class.
class Derive: public Base
{
public:
Derive()
{
}

void Draw()
{
cout << "Derive.Draw()\n";
}
};

//Define the name space to use.
using namespace std;


//Main method. C++ program beings execution here.
int main(int argc, char *argv[])
{
vector<Base> temp;


//Add derive object.
Derive d1;
temp.push_back( d1 );

//Add another derive object.
Derive d2;
temp.push_back( d2 );

//Loop through the vector.
for (int x=0; x < temp.size(); x++)
{
temp[x].Draw();
}

//Pause the DOS command window.
system("PAUSE");

//Return system status.
return 0;
}
John Carson
Guest
 
Posts: n/a
#2: Jul 22 '05

re: HELP: vector & polymorphism


"The Directive" <the_directive@hotmail.com> wrote in message
news:8477bc58.0312212044.128954de@posting.google.c om[color=blue]
> I can't understand why in this folling example the message
> "Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because
> of polymorphism? How can I rewrite this example using a vector to
> achieve polymorphism. Help!!! I'm very confused.
>
> //Base class.
> class Base
> {
> public:
> Base()
> {}
>
> virtual void Draw()
> {
> cout << "Base.Draw()\n";
> }
> };
>
> //Derive class.
> class Derive: public Base
> {
> public:
> Derive()
> {
> }
>
> void Draw()
> {
> cout << "Derive.Draw()\n";
> }
> };
>
> //Define the name space to use.
> using namespace std;
>
>
> //Main method. C++ program beings execution here.
> int main(int argc, char *argv[])
> {
> vector<Base> temp;
>
>
> //Add derive object.
> Derive d1;
> temp.push_back( d1 );
>
> //Add another derive object.
> Derive d2;
> temp.push_back( d2 );
>
> //Loop through the vector.
> for (int x=0; x < temp.size(); x++)
> {
> temp[x].Draw();
> }
>
> //Pause the DOS command window.
> system("PAUSE");
>
> //Return system status.
> return 0;
> }[/color]

Polymorphism only works through pointers or references. It doesn't work
through objects. Change your code to:

int main(int argc, char *argv[])
{
vector<Base*> temp;

//Add derive object pointer.
Derive d1;
temp.push_back(&d1);
//Add another derive object pointer.
Derive d2;
temp.push_back(&d2);

//Loop through the vector.
for (int x=0; x < temp.size(); x++)
{
temp[x]->Draw();
}
//Pause the DOS command window.
system("PAUSE");

//Return system status.
return 0;
}


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Thierry Miceli
Guest
 
Posts: n/a
#3: Jul 22 '05

re: HELP: vector & polymorphism


[color=blue]
> //Main method. C++ program beings execution here.
> int main(int argc, char *argv[])
> {
> vector<Base> temp;[/color]
temp will hold Base objects only!!!!!
[color=blue]
>
>
> //Add derive object.
> Derive d1;
> temp.push_back( d1 );[/color]
d1 is copied to a Base object
[color=blue]
>
> //Add another derive object.
> Derive d2;
> temp.push_back( d2 );[/color]
Same thing here
[color=blue]
>
> //Loop through the vector.
> for (int x=0; x < temp.size(); x++)
> {
> temp[x].Draw();[/color]
Base::Draw is called sinze temp[x] is a Base object


Suggested solution:

int main()
{
vector<Base*> temp;

// Dynamically create a Derive instance and add it to the vector
temp.push_back( new Derive);

//... add other Derive instances to vector as needed

for (size_t x=0; x < temp.size(); x++)
{
// Here, Derive::Draw is called because temp[x] is actually a pointer to
a Derive object and Base::Draw is virtual
temp[x]->Draw();
}

// Don't forget to delete dynamically created objects
for ( x=0; x < temp.size(); x++)
{
delete temp[x];
}

return 0;
}


Thierry


Cy Edmunds
Guest
 
Posts: n/a
#4: Jul 22 '05

re: HELP: vector & polymorphism


"The Directive" <the_directive@hotmail.com> wrote in message
news:8477bc58.0312212044.128954de@posting.google.c om...[color=blue]
> I can't understand why in this folling example the message
> "Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because
> of polymorphism? How can I rewrite this example using a vector to
> achieve polymorphism. Help!!! I'm very confused.
>
> //Base class.
> class Base
> {
> public:
> Base()
> {}
>
> virtual void Draw()
> {
> cout << "Base.Draw()\n";
> }
> };
>
> //Derive class.
> class Derive: public Base
> {
> public:
> Derive()
> {
> }
>
> void Draw()
> {
> cout << "Derive.Draw()\n";
> }
> };
>
> //Define the name space to use.
> using namespace std;
>
>
> //Main method. C++ program beings execution here.
> int main(int argc, char *argv[])
> {
> vector<Base> temp;
>
>
> //Add derive object.
> Derive d1;
> temp.push_back( d1 );[/color]

At this point something rather nasty called "bit slicing" occurs. A Base
object is initialized with a Derive object. This is legal because a Derive
is a Base. However, any extra data contained in d1 is sliced off. This must
happen because temp can only hold Base objects. Thus the object which is
actually stored is a Base object, and any virtual functions will refer to
their Base incarnations.

One solution already mentioned by Thieri Miceli is to store Base pointers
rather than Base objects in the vector. This avoids bit slicing because a
Derive pointer is a Base pointer and, of course, both pointers are the same
size.

However, it does give you another problem -- where to come up with the
pointer. You can use the & operator but then you must have a place to store
the object whose address you are taking, and you must also make sure its
lifetime is at least as long as the pointer's. You can use the new()
operator but then you must take care to avoid memory leaks -- the vector
will not manage this memory for you.

A better alternative is to use a reference counted smart pointer. It looks
like this:

typedef boost::shared_ptr<Base> BasePtr;
std::vector<BasePtr> temp;
temp.push_back(BasePtr(new Derive));

The advantage of this arrangement is that the smart pointer will manage the
memory for you. Get boost::shared_ptr at

www.boost.org

Aside to newsgroup: What has to be done to get this into the FAQ? It
certainly is asked frequently.

[snip]

--
Cy
http://home.rochester.rr.com/cyhome/


The Directive
Guest
 
Posts: n/a
#5: Jul 22 '05

re: HELP: vector & polymorphism


[snip]

Thanks to all. Cy, your explanation was very helpful.
[color=blue]
> At this point something rather nasty called "bit slicing" occurs. A Base
> object is initialized with a Derive object. This is legal because a Derive
> is a Base. However, any extra data contained in d1 is sliced off. This must
> happen because temp can only hold Base objects. Thus the object which is
> actually stored is a Base object, and any virtual functions will refer to
> their Base incarnations.
>
> One solution already mentioned by Thieri Miceli is to store Base pointers
> rather than Base objects in the vector. This avoids bit slicing because a
> Derive pointer is a Base pointer and, of course, both pointers are the same
> size.
>
> However, it does give you another problem -- where to come up with the
> pointer. You can use the & operator but then you must have a place to store
> the object whose address you are taking, and you must also make sure its
> lifetime is at least as long as the pointer's. You can use the new()
> operator but then you must take care to avoid memory leaks -- the vector
> will not manage this memory for you.
>
> A better alternative is to use a reference counted smart pointer. It looks
> like this:
>
> typedef boost::shared_ptr<Base> BasePtr;
> std::vector<BasePtr> temp;
> temp.push_back(BasePtr(new Derive));
>
> The advantage of this arrangement is that the smart pointer will manage the
> memory for you. Get boost::shared_ptr at
>
> www.boost.org
>
> Aside to newsgroup: What has to be done to get this into the FAQ? It
> certainly is asked frequently.[/color]

It should definetely be in the FAQ.
[color=blue]
> [snip][/color]

--The Directive
Closed Thread