Notes for Traversing container objects in C ++

Source: Internet
Author: User

Suppose there is a container actormanager for such a management object, and its implementation is roughly

class Actor;
class ActorManager
{
public:
void update()
{
for (actors_t::const_iterator itr = m_actors.begin(); itr != m_actors.end(); ++itr)
{
Actir* actor = itr->second;
actor->update();
}
}

void add(Actor* actor)
{
m_actors[actor->get_id()] = actor;
}

void remove(Actor* actor)
{
m_actors.erase(actor->get_id());
}

private:
typedef std::map actors_t;
actors_t m_actors;
};

 

The actor class is implemented as follows:

class Actor
{
public:
void update()
{
// ...

}

One day, when adding logic to the actor, the update function becomes like this

void update()
{
// ...

update_buff_effect();

// ...
}

 

Go down

 

class Actor
{
// ...

private:
void update_buff_effect()
{
// ...

apply_hp(-100);
if (get_hp() <= 0)
{
die();
return;
}

// ...
}

 

Then ......

 

private:
void die()
{
// ...

ActorManager::getInstance().remove(this);

// ...
}
When I wrote down actormanager, I didn't expect to delete objects in the update loop, But I encountered similar problems several times.
Some problems are not so obvious, but they are also in the process of traversing the container object. An execution function deletes the objects in the window, leading to the failure of the iterator.
 
The modification method is simple. Add a list of objects to be deleted to actormanager.
In the Remove Method, objects are not deleted, but are deleted after the loop in update.
The code looks like this:
 
class Actor;
class ActorManager
{
public:
void update()
{
m_is_looping = true;
for (actors_t::const_iterator itr = m_actors.begin(); itr != m_actors.end(); ++itr)
{
Actir* actor = itr->second;
actor->update();
}
m_is_looping = false;

if (!m_removed_actors.empty())
{
for (removed_actors_t::const_iterator itr = m_removed_actors.begin();
itr != m_removed_actors.end(); ++itr)
{
Actor* actor = *itr;
m_actors.erase(actor->get_id());
}
m_removed_actors.clear();
}
}

void add(Actor* actor)
{
m_actors[actor->get_id()] = actor;
}

void remove(Actor* actor)
{
if (!m_is_looping)
m_actors.erase(actor->get_id());
else
m_removed_actors.push_back(actor);
}

private:
typedef std::map actors_t;
actors_t m_actors;

typedef std::vector removed_actors_t;
removed_actors_t m_removed_actors;
bool m_is_looping;
};

 

The reason why add is not protected is that a new object is not added to actormanager in the update function.

Of course, there may be such a demand elsewhere, and similar protection can also be done.

 

 

Although the problem is not big, it has encountered similar errors several times. Record and force yourself,

In the event of such problems, we will do for… for the objects in the container... During Processing, be sure to carefully check the remove interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.