3

I have an abstract base class in C++ and need to create an array to store objects which are subclasses of it. I use pointers to the subclasses since each member of the array needs to be of the same size. Currently I am declaring and initializing my array like this:

BaseClass *array[];
...
array =
{
    &SubClass1(...),
    &SubClass2(...),
    ...
    &SubClass3(...)
};

This is giving me the following when I try to compile:

warning: taking address of temporary
error: too many initializers for ‘BaseClass* [0]’

What's the proper way of doing this?

PandaConda
  • 3,396
  • 2
  • 20
  • 22

3 Answers3

5

You cannot do it with temporaries - you should allocate your objects statically, dynamically, or put them in the automated storage.

To allocate objects statically, do this:

static SubClass1 s1(..);
static SubClass2 s2(..);
static SubClass3 s3(..);
BaseClass *array [] = { &s1, &s2, &s3 };

If you allocate them dynamically, consider using a vector<shared_ptr<T>> instead of "raw" pointers to automate deallocation:

vector<shared_ptr<BaseClass>> data = {
    make_shared<SubClass1>(...)
,   make_shared<SubClass2>(...)
,   make_shared<SubClass3>(...)
};
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • `std::make_shared(...)` would even be better ! – Johan Nov 21 '13 at 17:37
  • Thanks. I'll try this method. So is there no way of doing this without also storing each (pointer to each) object in a separate variable outside the array? I would like to be able to just edit the contents of this array in order to change its contents without having to go modify anything outside of it. – PandaConda Nov 21 '13 at 17:53
  • Another answer worked before I had a chance to try this. Thanks for the help :) – PandaConda Nov 21 '13 at 17:55
  • @PandaConda The second way (with smart pointers) lets you do it without making external objects, and taking their addresses. However, even if you do create external objects (e.g. in the way shown in the first code snippet) you could "forget" that you have objects `s1`..`s3`, and do all your manipulations through the `array`. – Sergey Kalinichenko Nov 21 '13 at 17:55
2

Those SubClass1(...) are temporary objects and if the control goes out of the scope they will be destructed and all pointers of array will be dangling pointer to invalid addresses.

You can make those objects in array's scope to make sure those objects and this array have same lifetime:

SubClass1 class1 (...);
SubClass2 class2 (...);
SubClass3 class3 (...);
BaseClass *array[] =
{
    &class1(...),
    &class2(...),
    ...
};

Also you can use smart pointer:

std::unique_ptr<BaseClass> array[] =
{
    std::unique_ptr<BaseClass>(new SubClass1),
    std::unique_ptr<BaseClass>(new SubClass2)
};
masoud
  • 55,379
  • 16
  • 141
  • 208
  • I do not think `std::unique_ptr` would be good if it's a sort of factory design because you cannot copy them... But if it's just for access, that will work. – Johan Nov 21 '13 at 17:43
2

On the same line and not taking the address of the temporary as you're doing in &SubClass1(...),. Something, like:

BaseClass *array [] = { new SubClass(...), new SubClass2(...), .... };

But this design smells a bit like not knowing how to do a factory.

As you're not using std::shared_ptr or std::unique_ptr to manage your pointer, do not forget to delete them ! (thanks @dasblinkenlight)

Johan
  • 3,728
  • 16
  • 25