Re: using map, list etc. in const methods

From: Donovan Rebbechi (abuse_at_aol.com)
Date: 01/23/04


Date: Fri, 23 Jan 2004 19:26:30 +0000 (UTC)

In article <burrlk$j5g$07$1@news.t-online.com>, Christof Krueger wrote:
> Hello,
>
> I'm quite new to C++ so maybe there's something I miss.
> I write a simple board game. It has a board class. This class has a
> method that returns the count of pieces a player has on the board. Since
> this function does not change anything in the class I declared it as
> const. To count all pieces of a given color the functions iterates
> through a "map" of CNode-pointers. "CNode" is another class that is
> irrelevant to the problem.
>
> Here comes the function: (EColor is an enum of colors, not important here)
>
> int
> CBoard::countPieces(EColor color) const
> {
> // "nodes" is a private class member
> // with type map<string,CNode*>
> int count = 0;
> for (map<string,CNode*>::iterator i=nodes.begin();
> i != nodes.end();
> i++)
> {
> // do something with (*i)
> // ...
> }
> return count;
> }
>
> The compiler refuses to compile the code, because I violate the
> constness of the function in the line where I declare the iterator.
> Is there a proper way (or workaround) to leave the function const?

map<string,CNode*>::const_iterator

btw, you can do this using

count_if(themap.begin(),themap.end(),isWhite); // isWhite returns true if the piece is white

> Another point: I've heard, that it is not good to store pointers to
> objects in stl-containers like map, list, vector etc. because of
> ownership problems (?).

It's not necessarily a bad thing. But you need to make sure that you understand
who "owns" a given pointer, and you need to make sure that the map doesn't hold
on to "dead pointers".

Here's my question: how are your pointers allocated and deleted ? Who owns them ?

> Do you know a good webpage or tutorial about
> this topic? I would like to learn how to do it the right way.

This topic is too complicated to be discussed exhaustively in "a webpage". You
need to read a couple of good books that deal with memory management issues.

> There are a lot of classes in the STL and I would like to know how to
> use their whole power. At the moment I'm just using lists, vectors and
> maps in lack of a good book or online tutorial discussing the STL in detail.

Two good books about the C++ standard library:

"Accelerated C++" Koenig and Moo
"The Standard C++ Library A Tutorial and a Reference" Josuttis

Cheers,

-- 
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/


Relevant Pages

  • Re: Vinland map and Historia Tartarorum, question
    ... Where are the documents with that sand? ... seacharts, books, ... ... working with VM before the map was analyzed. ... presentation of anatase found on VM and what you believe to be 1920's 'industrial' anatase the scales aren't the same. ...
    (sci.archaeology)
  • Re: Problem with CMapStringToPtr s Lookup problem
    ... Map variable "chat" ... //getting the vector's refrence from Map ... All pointers to such variables are invalid and your program's behaviour will be undefined. ...
    (microsoft.public.vc.mfc)
  • Re: Strategies for avoiding new?
    ... > container is important (which is why I use map or whatever.) ... from the container and manipulate it directly. ... So after my snippet, ... using pointers is unnecessary. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Umbrarum Regnum
    ... looks like the basic idea in your current framework is that pointers are ... The game was actually ... Therefore it would be easy to add a "rewind time" feature and I'm ... so that the player doesn't even see the map ...
    (rec.games.roguelike.development)
  • Re: Origin of the VM
    ... > seller of rare books, Enzo Feriole. ... the prosecution claimed that Ferrajoli had charmed ... manuscript of the Tartar Relation and bound it together with a map. ...
    (sci.archaeology)