This answer explains that what happens when we create/remove object with and without new/delete. Now consider three object top, mid and bottom. top is created WITHOUT new, and it goes out of scope only when whole program ends. Now within top there is and infinite while loop which keep creating and deleting mid objects by using new and delete. And on creation mid objects create some bottom objects within them, WITHOUT new, which are never deleted explicitly.
My question is, will bottom objects be automatically deleted on delete call on mid? It seems scope of bottom is in whole top. So every created bottom object will never go out of scope. Right?
Sample Code:
Main:
Top();
Top.h:
class Top{
public:
vector<Mid> midVec;
}
Top::Top(){
canExit = false;
run();
}
Top::run(){
while(!canExit){
//dosomething
midVec.push_back(new Mid(););
//againdosomemorething - they use Mid and things within it.
vector<Mid>::iterator b, e;
//iterate over midVec check for some condition and identify some to delete.
// call ***delete*** on identified objects.
}
}
Mid::Mid(){
for(int i =0; i<someCount; i++){
Bottom obj = Bottom();
umB.insert(std::make_pair<int, Bottom>(i, obj));
umBPTR.insert(std::make_pair<int, Bottom*>(i, &obj));
}
}
Mid.h
class Mid{
public:
unordered_map<int, Bottom> umB;
unordered_map<int, Bottom*> umBPTR;
}