HELP: vector & polymorphism

T

The Directive

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;
}
 
J

John Carson

The Directive said:
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;
}

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;
}
 
T

Thierry Miceli

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

Cy Edmunds

The Directive said:
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 );

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]
 
T

The Directive

[snip]

Thanks to all. Cy, your explanation was very helpful.
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.

It should definetely be in the FAQ.

--The Directive
 

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
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top