473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

HELP: vector & polymorphism

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;
}
Jul 22 '05 #1
4 2551
"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 );

//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;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
//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
Jul 22 '05 #3
"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/
Jul 22 '05 #4
[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.
[snip]


--The Directive
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
14
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
4
by: helge | last post by:
What is the best way to implement a vector in space R3, i.e a vector holding three floats, supporting arithmetic operations, dot and cross product etc in c++? is there a standard library class for...
5
by: rsennat | last post by:
Hi All, Here is the basic example of the using polymorphism. And then my question is below. typedef std::vector<Vehicle*> VehicleList; void myCode(VehicleList& v) { for...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.