"The Directive" <th***********@hotmail.com> wrote in message
news:84**************************@posting.google.c om...
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]
--
Cy
http://home.rochester.rr.com/cyhome/