without requiring a default constructor or copy constructor. |
A copy constructor is already going to be a requirement for a vector style container because you'll have to copy/move all the objects when the array needs to be reallocated. So that's pretty much going to be expected. Copy (or at least Move) constructors are required for any objects you put in an std::vector.
And since it's already an expected requirement, you might as well use it when adding items to the vector initially.
The only way around it that I can think of would be to have some kind of callback mechanism where the user can provide their own function to construct the object. But talk about overly complicated and counter-intuitive.
Since the memory class has the construct method (seems standard), how does it know where to put the objects? |
I'm not sure I know what you mean by "the memory class". Are you talking about the vector class?
Anyway... there are two different parts to object creation:
1) allocating the memory for the object(s)
2) calling the object constructor.
Likewise, teardown consists of the same 2 parts, reversed:
1) calling the object destructor
2) freeing memory for the object(s)
std::vector allocates space for 'capacity' objects. So if each object is 16 bytes, and you have capacity=16... it will have 256 bytes allocated.
As elements are added to the vector, it will begin constructing objects (using placement new, or really an 'std::allocator' interface which typically just wraps placement new) within that allocated memory.