first question: In Approach A, is a memory leak created?
Yes.
second question: assuming A does produce a memory leak, does B solve it?
Yes, but it's a silly solution. And unsafe in the event that the DD_DungeonRoom
's copy constructor or vector::push_back
throws an exception.
third question: is there (or more likely, 'what is') a better way to add custom class objects (e.g. requiring dynamic allocation via 'new' or 'malloc') to a vector by value?
No objects in C++ require dynamic memory allocation. Just add objects directly to the vector calling the constructor, sans new
.
mvLayoutArray.push_back(DD_DungeonRoom());
Even better, if your compiler supports the feature (it is new to C++11), would be to use emplace_back
which completely bypasses any copies, and constructs your object directly in the vector. Just pass the same arguments to it as you would to your constructor. In our case, that's none:
myLayoutArray.emplace_back();