• Advertisement
Sign in to follow this  

Polymorphic array.

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lets say I have a base class, 'Widget'. What I want to be able to do create several objects of different types, which are all derived from 'Widget', and be able to store them in an array (or other suitable container class). Is this possible, and if not, are there any suitable workarounds? Example:

class Base
{};

class One: public Base
{};

class Two: public Base
{};

One X;
Two Y;

std::vector<Base> Storage;

Storage.push_back(X);
Storage.push_back(Y);



I know its possible to pass a derived class into a function instead of the parent class, I just dont know how to apply this to another situation, such as this one. PS: I apologise if my wording is incorrect.

Share this post


Link to post
Share on other sites
Advertisement
It is customary to simply store a base class pointer in the array, and use that polymorphically.


class Base
{};

class One: public Base
{};

class Two: public Base
{};

std::vector<Base*> Storage;

Storage.push_back(new One);
Storage.push_back(new Two);



Use of a smart pointer would be preferred though.

Share this post


Link to post
Share on other sites
The reason why you cannot have a vector of Base and use it polymorphically is that every object of a derived class will take up different amounts of bytes, while Base* is always of constant size (4 bytes on 32 bit machines and 8 bytes of 64 bit I believe).

If you but a derived object in a vector of Base it gets truncated to a Base and is not derived anymore.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathkrush
Don't store pointers in a std::vector, it's a bad idea. Try Boost Pointer Container, it's made specifically for that task.


Why is it a bad idea? I do it all the time and the only problems I've had is when I've forgotten to do something important.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
Quote:
Original post by deathkrush
Don't store pointers in a std::vector, it's a bad idea. Try Boost Pointer Container, it's made specifically for that task.


Why is it a bad idea? I do it all the time and the only problems I've had is when I've forgotten to do something important.
It handles all of the pointer nastiness for you, and is (marginally) more efficient than std::vector< boost::shared_ptr< Base > > >.

Share this post


Link to post
Share on other sites
Quote:
Original post by jdhardy
Quote:
Original post by Endar
Quote:
Original post by deathkrush
Don't store pointers in a std::vector, it's a bad idea. Try Boost Pointer Container, it's made specifically for that task.


Why is it a bad idea? I do it all the time and the only problems I've had is when I've forgotten to do something important.
It handles all of the pointer nastiness for you, and is (marginally) more efficient than std::vector< boost::shared_ptr< Base > > >.


In other words, it takes care of something important (memory deallocation) automatically so you can't froget to do it. Humans are frogetful creatures, anything automatable by the system is a good thing in terms of avoiding bugs :-).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement