What I am trying to do is create a base class, for which anything derived from it will automatically be registered with another class, eg:
class System;
class Object
{
public:
Object()
{
System sys;
sys.AddObject(this);
}
virtual ~Object()
{
System sys;
sys.RemoveObject(this);
}
};
class System
{
public:
// Some other processing function which operates on all things derived
// from Object at one time.
void ProcessAllObjectsInExistence();
void AddObject(Object *o)
{
list.push_back(o);
}
void RemoveObject(Object *o)
{
std::vector<Object *>::iterator i = find(list.begin(), list.end(), o);
if (*i != list.end()) list.erase(i);
}
private:
static std::vector<Object *> list;
};
Is this legal and defined? The reason why I am asking is because I am getting some funny errors after reading and writing to the data members of Object
(not shown here) from inside ProcessAllObjectsInExistence()
.
i.e. for a single Object
before I call System.ProcessAllObjectsInExistence()
the members are 0
and 100
, and then the first line inside ProcessAllObjectsInExistence()
is a debugging line which prints the members and displays 0
and 0
. painful ><