Vinícius
Vinícius

Reputation: 15746

Point to an object on a container

Is this a valid good practice? (assume that ARENAPLAYER is a struct)

ARENAPLAYER* CArena::GetPlayer( u_long idPlayer )
{
    map<u_long, ARENAPLAYER>::iterator it = m_mArenaMap.find( idPlayer );
    if( it != m_mArenaMap.end() )
        return &it->second;
    return NULL;
}

If it is, will the pointer point directly to the object inside the map? Any changes made by the pointer will change the object in the map?

Upvotes: 2

Views: 98

Answers (2)

Yuushi
Yuushi

Reputation: 26080

There is absolutely no reason to do this. operator[] returns a reference to the value stored, allowing you to change values without mucking around with pointers.

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154035

You can get pointers to the objects inside the std::map<u_long, AREANPLAYER> like this. The objects inside a map stay put as long as the map lives and the object isn't erased from the map.

Upvotes: 3

Related Questions