I am trying to add my object pointer into vector same time object is created. Currently I have done it in object's constructor but now I have heard that this is a wrong way because object isn't completely created on that point.
MyObject class constructor:
// MyObject constructor
MyObject::MyObject() {
// add object to vector of all objects
MyObjectManager::Instance()->objects.push_back(this);
}
MyObject is just a base class for objects. I have also MyRectangle class that is inherited from MyObject so when I create new MyRectangle then MyObject constructor is called and my newly created object is pushed into MyObjectManager's vector.
MyObjectManager is a singleton class that keeps list of all objects and calls their virtual Draw function very frequently. Is that the problem? MyObjectManager might call object's Draw function before object is completely created?
I could make separate method for adding object to vector. Something like this:
MyObject::Create() {
// add object to vector of all objects
MyObjectManager::Instance()->objects.push_back(this);
}
But then I have to use it like this:
MyRectangle *rect = new MyRectangle(0.5, 0.5, 0.1, 0.1);
rect->Create();
I just want to be able to instantiate new object simply by constructor like this:
MyRectangle *rect = new MyRectangle(0.5, 0.5, 0.1, 0.1);