How do I delete the space I allocated with new when I need to return the result. I used this so if I don't find an object I return back an object with a dummy sentinel value.
ClassObject* ClassObjectTwo::find(string findid) {
ClassObject *sendback;
bool found;
for(vector<ClassObject>::iterator it = data.begin(); it != data.end(); it++) {
if ( it->get_id() == findid) {
found = true;
sendback = &(*it);
}
}
if(!found) {
sendback = new ClassObject;
sendback->set_key(100);
}
return sendback;
}
Or is this not a problem as its destroyed when out of scope. The only other solution I can think of is place the object in the constructor and delete via the constructor. I just didn't want to add a variable for one function.
It is perfectly fine to destroy a dynamically created object outside the function.
However, in this case it would be better to return NULL.