[edit]Boo for beating me. By the way, does std::vector
require a default constructor? I suppose it just internally allocates bytes and uses placement new as well, though, so probably not. If that's the case, then yeah, I agree, use std::vector. I would've mentioned it before placement new, but I was thinking that it required the existence of a default constructor.[/edit]
Well, it wouldn't be as clean looking, but you could make use of the placement new operator. I haven't used it myself before, but I think I'm familiar enough to give you a piece of code that should supposedly work:
cClass *ArrayOfClass;//AllocationArrayOfClasses = (cClass*)(new char[sizeof(cClass) * NumElements]);for (int i = 0; i < NumElements; ++i) new (ArrayOfClasses + i) cClass(g_Device);//... Use array normally//Deallocationfor (int i = 0; i < NumElements; ++i) (ArrayOfClasses + i)->~cClass();delete [] (char*)ArrayOfClasses;
Placement new doesn't actually allocate memory, it merely constructs a new object in memory that is already allocated. So you allocate a byte array that's large enough for all your objects, call the appropriate constructor on each element using placement new; then when you're done, you manually call the destructor of each element, and then delete the byte array to free the memory.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke