I'll have a large struct containing basic types, STL objects (std::string) as well as objects of a library class (which I can't modify).
struct Info {
string name;
int number;
LibClass object;
// ...
}
I'll want to stuff instances of this struct into a vector and then create a function to fill this vector.
vector<Info> list;
void addElement (string myName, int myNumber, LibClass myObject /*, ... */)
{
Info newElement = { myName, myNumber, myObject /*, ... */};
list.push_back(newElement);
}
The above code will fail, because the LibClass does not have an assigment operator specified which is needed by the vector implementation.
The code will work if I change my struct (and the addElement
function parameter) to hold a pointer to a LibClass object instead. But then I would have to do memory management outside of my function (create an LibClass object on the heap for every new element).
Next best thing is to do the memory management inside the addElement
function by creating a copy of the given stack object on the heap, which would keep my calling code clean. But as this code is not part of another object, I have no destructor where I could free the memory again.
Is there a way to implement this without having to do memory allocation outside of this code AND without blowing this up to be a full featured factory class?
Update: I can't modify the library class, I'm not able to use C++11 or helper libs such as boost. The structs will basically be read-only inside the vector. Even the vector will be only filled once, then only read access will happen.