Is "PositionInVector" used for anything except deciding which child to remove? Because you could just use something like this:
class Object{ typedef std::vector<Object *> Children;public: void addChild(Object *child) { children.push_back(child); child->parent = this; } // use: parent->removeChild(this); void removeChild(Object *child) { Children::iterator it = std::find(children.begin(), children.end(), child); if(it != children.end()) { children.erase(it); } }private: Children children;};
Be careful though. It appears that with your original code the child is deleted inside the removeChild() function, which looks very dangerous. If you had some explicit way to manage the ownership, such as smart pointers, it would probably be better.
An approach like the above or your original runs the risk of pointer related bugs, especially for a beginner.
A totally different approach would be to let the parent handle the lifetime of the Objects. For example, if the objects have a boolean function isDead(), you could do this:
class Object{ typedef boost::shared_ptr<Object> ObjectPtr; typedef std::vector<ObjectPtr> Children; static bool dead(const ObjectPtr &ptr) { return ptr->isDead(); }public: bool isDead() const; // could be virtual or whatever void addChild(ObjectPtr child) { children.push_back(child); } void update() { // remove dead children Children::iterator it = std::remove_if(children.begin, children.end(), dead); children.erase(it, children.end()); // ... }};
And for future note: an hour is actually quite a short time for a forum. You should wait a *lot* longer before bumping. Certain times are busier than others, so someone will probably see your post before long.