I'm trying to dynamically allocate an array of objects. Each of my objects have a parameter in their constructor that must be initialized. I would prefer to initialize them at construction time, because I think it would save time. How do I initialize an array of dynmaically allocated objects at allocation time, with a constructor that requires a parameter?
class Thingy{
private:
int* a;
public:
Thingy(int size);
};
class ThingyLayer{
private:
Thingy* m_things;
public:
ThingyLayer(int n){
m_things = new Thingy[n]; //!< How do I pass a param here to the ctor of Thingy
}
};
I would prefer not to use std::vector
in this case, because some day I might want to run this on an embedded system that doesn't support STL, like the Atmel AVR chips. So I am looking for how to do this with pointers.
I've already tried, m_things = new Thingy[n](val)
, but that doesn't work as it raises a compiler warning. I also viewed Dynamically allocating an array of objects, but that did not answer my question.