I have this issue for which I haven't been able to find an answer through Bing or Google. My RobotExtension class's constructor takes a container of pointers to append to its own member container. However, the derived class dynamically allocates a default set of pointers in the member container.
This means that I am deallocating memory in the destructor, but what if the pointers passed to the constructor were not pointing to dynamically allocated objects? Is there a way to check if what a pointer is pointing to is on the stack or heap?
// deallocation occurs on line 25 | allocation occurs on line 40
/* Clarification:
RobotPart is a separate class
PartsList__ is a list<RobotPart*> member object
Link__ is a list<RobotPart::Connection_t> object
Connection_t is an enum nested in RobotPart
__Connection is of type Connection_t and is a member variable of RobotPart
*/
RobotExtensions::RobotExtensions(const list<RobotPart*>& newparts){
PartsList__.resize(Partslist__.size() + newparts.size());
for(auto part___ = newparts.begin(); part__ != newparts.end(); part___++){
//Check if there is an available connection
if(Link__.empty()) break;
for(auto alink__ = Link__.begin(); alink__ != Link__.end(); alink++){
if((*alink) == (*part__)->__Connection){
PartsList__.pushback(*part___);
Link__.erase(alink);
break;
}
}
}
}
RobotExtensions::~RobotExtensions(){
for(auto part__ = PartsList__.begin(); part__ != PartsList__.end(); part__++)
delete (*part);
}
/******************Derived Class**********************/
/* Clarification
Frame is a derived class of RobotExtensions
RE_Name is a member string object of RobotExtensions
SmartBar is a derived class of RobotPart
Male_Female and Snap are enumerations of Connection_t
*/
Frame::Frame(const list<RobotPart*>& newparts):RobotExtensions(newparts){CreateDefault();}
void Frame::CreateDefault(){
constint minimum_bars_need(10);
RE_Name__ = "Frame";
for(int i = 0; i < minimum_bars_need; i++){
PartsList__.push_back(new SmartBar());
Link__.push_back(Male_Female);
}
for(int i = 0; i < minimum_bars_need/2; i++)
Link__.push_back(Snap);
}