C ++ creates an array of objects (from different classes)

I need to create an array containing objects from several classes.

Example

class baseClass
{
   //
};

class first : baseClass
{
   //
};

class second : baseClass
{
   //
};

How to create an array that can contain an object firstand / or secondinside it?

This is part of the home task for me, so I have to use arrays, I have already searched and know that this is done with the help of additional libraries and such, but I have no choice here, guys ...

+5
source share
3 answers
baseClass *array[10];
baseClass **array2 = new baseClass *[size];

. , . , .

std::vector<baseClass*> vec;

, , baseClass.

std::vector<boost::variant<first,second> > vec2;

, , . .

std::vector<std::unique_ptr<baseClass>> vec3;

, , , . ++ 11.

, , std::array<...,size>

std::array<std::unique_ptr<baseClass>,10> array3; baseClass *array[10]; . ( , )

+2

- Boost, ++ 11 - . "" . . , .

+6

, , :

baseClass *array[123];
array[0] = new first();
array[1] = new second();

. ( delete )

+2

All Articles