For my project I need to store pointers to objects of type ComplicatedClass
in an array. This array is stored in a class Storage
along with other information I have omitted here.
Here's what I would like to do (which obviously doesn't work, but hopefully explains what I'm trying to achieve):
class ComplicatedClass
{
...
}
class Storage
{
public:
Storage(const size_t& numberOfObjects, const std::array<ComplicatedClass *, numberOfObjects>& objectArray)
: size(numberOfObjects),
objectArray(objectArray)
{}
...
public:
size_t size;
std::array<ComplicatedClass *, size> objectArray;
...
}
int main()
{
ComplicatedClass * object1 = new ComplicatedClass(...);
ComplicatedClass * object2 = new ComplicatedClass(...);
Storage myStorage(2, {object1, object2});
...
return 0;
}
What I am considering is:
- Using
std::vector
instead ofstd::array
. I would like to avoid this because there are parts of my program that are not allowed to allocate memory on the free-store. As far as I know,std::vector
would have to do that. As a plus I would be able to ditchsize
. - Changing
Storage
to a class template. I would like to avoid this because then I have templates all over my code. This is not terrible but it would make classes that useStorage
much less readable, because they would also have to have templated functions.
Are there any other options that I am missing?