I can see that you have posted too many times, most likely by accident since GDNet is pretty unstable currently, you can remove the extra posts by pressing edit and then deleting it.
Quote:Original post by by
1- Why, man->update() didn't generate any line?
You never set r to anything so it gets some random (well almost) value, most likely bigger than objlist.size() and therefore it never enters the loop. You should do like this:
for (int r = 0; r<objlist.size(); r++){ cout << "Object" << r << "'s value is: " << objlist[r].getvalue() << endl; }
Also you should read about iterators which is a better way to iterate through a container. It will also work with lists and might provide a performance improvement with some containers.
Quote:2-
CObject created! (17),
CObject deleted! (17)
CObject deleted! (17)
So, c++ runs ~CObject for more then a time?
Well, yes, but it also creates another CObject. If you want a surprise, try changing your main to:
int main(int argc, char *argv[]){ CObjectManager *man = new CObjectManager; CObject *obj = new CObject(17); CObject *obj2 = new CObject(21); cout << "1" << endl; man->addObject(*obj); cout << "2" << endl; man->addObject(*obj2); cout << "3" << endl; //obj2 = &man->getobject(0); cout << obj->getvalue() << endl; man->update(); system("PAUSE"); return EXIT_SUCCESS;}
When you copy an object you will invoke it's copy constructor, which in this case is created by the compiler for you. Your CObject actually have a constructor which looks like this:
CObject::CObject(const CObject& copy){value = copy.value; }
So at the moment you invoke addObject a new CObject object is created, when you exit the function it's destroyed, so here you will get an extra call to the destructor. If you want to understand this better try adding the following method to CObject
CObject::CObject(const CObject& copy){value = copy.getvalue()*10; // Multiply by 10 to get a new valuecout << "CObject copied original=" << copy.getvalue() << " new=" << value<< std::endl; }
I think you will understand your code much better if you try to do that. Another thing you should note is that std::vector makes a copy of your CObject, this is the reason for the second call to the destructor. The two objects you originally created (obj and obj2) will never be destroyed because you never calls delete on them. So right before system("PAUSE") you should insert the following:
delete man;delete obj;delete obj2;