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);
Polymorphic array.
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:
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.
It is customary to simply store a base class pointer in the array, and use that polymorphically.
Use of a smart pointer would be preferred though.
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.
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.
If you but a derived object in a vector of Base it gets truncated to a Base and is not derived anymore.
Don't store pointers in a std::vector, it's a bad idea. Try Boost Pointer Container, it's made specifically for that task.
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.
Quote:Original post by EndarIt handles all of the pointer nastiness for you, and is (marginally) more efficient than std::vector< boost::shared_ptr< Base > > >.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.
Quote:Original post by jdhardyQuote:Original post by EndarIt handles all of the pointer nastiness for you, and is (marginally) more efficient than std::vector< boost::shared_ptr< Base > > >.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.
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 :-).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement