So, I have an object with a constructor. This object doesn't support empty constructors because there are constants that must be assigned in the constructor. Something like this:
class Object {
public:
const int foo;
Object(int f) : foo(f) {
// other stuff
}
}
Now I have another function that keeps a dynamically allocated array of objects.
Object* objects;
I need to be able to allocate this memory using new[]
and delete[]
but also use the constructor. I can't seem to figure out how to do this.
Something like this would be ideal
objects = new Object[numObjects];
for (int i = 0; i < numObjects; ++i) {
object[i](foos[i]);
}
I could use malloc
and free
, then new
the objects in place, but that would be non ideal as I would have to loop through the objects in the destructor and manually destruct them, then free the memory. Seems messy.
Aucun commentaire:
Enregistrer un commentaire