Skip to main content
GameDev.net gamedev.net
🔒 Locked

Polymorphic array.

Started by Kustaz May 6, 2006 at 1:57 AM 6 replies 1.2k views
Original Post
Kustaz
Kustaz
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.
Mxz
Mxz
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.
Fred304
Fred304
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.
Zahlman
Zahlman
Indeed. [google] "object slicing C++" for more.
deathkrush
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.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Endar
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
jdhardy
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 > > >.

MaulingMonkey
MaulingMonkey
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 :-).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.