Original Post
Hi there! I want to create an ObjectManager class, which handles freeing memory if objects are not longer used. But I just have a little problem with c++ documentation :) (I'm using g++ and SDL for a cross-platform project, so I cannot use VC++ thingies...) I'm just posting my "pseudocode" attempts in here for your review, perhaps you can help me some... objman.cpp (I'v just put it into one file for easier coding, seperating it later into objman.h and objman.cpp)
class CObjectManager {
Object[] ObjectList[10]; // array to store objects in
int numObjects; // number of stored objects
public:
explicit CObjectManager() {
add(this); // perhaps storing myself?
};
virtual ~CObjectManager() {
delete[] ObjectList; // sth like that to prevent mem-leakage
}
void add(Object* obj) {
ObjectList[numObjects] = obj; // add object pointer to array
numObjects++;
// here should be some check if array limits were exceeded
// and copy it into new memory area and free the old one (sth. like that)
}
void wipe() {
for (int i=numObjects; i>0; i--) {
delete *ObjectList; // delete object to which list entry points
}
numObjects = 0;
}
};
main.cpp
--- snip ---
CObjectManager *ObjectManager = new CObjectManager();
// Creating a new arbitrary Object which the objman should be able to handle
CMyObject *MyObject = new CMyObject();
MyObject->Blah();
MyObject->SetFoo(bar);
...
// one simple call should delete all objects
ObjectManager->wipe();
delete ObjectManager; // don't know how to let him delete himself
--- snip ---
CMyObject Constructor
CMyObject::CMyObject() {
ObjectManager->add(this);
}