Let's say I have a generic Object class, and a generic List class. I want to maintain a list of these Objects. Should I store them as List <Object> or List <Object>>?
If I use List <Object> and I have a method like:
if (some_condition) {
Object obj;
myObjectList.append (obj);
}
And my list class only keeps a reference to the object, so as soon as that if statement terminates, the object is destroyed, and the object I pushed becomes invalid. So then I end up doing something like:
Object * obj = new Object;
myObjectList.append (* obj);
So that it doesnt get destroyed. But now the objects are undeletable, no? Because now they're stored safely in the List as Objects, not pointers to Objects, so I can't call delete on them ... or will they automatically be destroyed when they're popped from the list?
In that case, I should probably use List <Object *> and delete them from the list when I'm done with them, no?
So confused ... I'm sure I have a fundamental misunderstanding here somewhere.
0 Comments