using map, list etc. in const methods

From: Christof Krueger (donotspamnews_at_pop2wap.net)
Date: 01/23/04


Date: Fri, 23 Jan 2004 20:12:11 +0100

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?

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 (?). Do you know a good webpage or tutorial about
this topic? I would like to learn how to do it the right way.
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.

Thanks in advance!

-- 
Regards,
   Christof Krueger
Remove "donotspam" if you want to email me.


Relevant Pages

  • Re: using map, list etc. in const methods
    ... > constness of the function in the line where I declare the iterator. ... > objects in stl-containers like map, list, vector etc. because of ... > There are a lot of classes in the STL and I would like to know how to ... At the moment I'm just using lists, ...
    (comp.lang.cpp)
  • Re: Question about STL containers in multithreaded environment
    ... If you examine each of the major STL implementations out there: ... SGI stl ... until I explicitly made sure the const version of find was being called. ... the map<> using a red-black tree. ...
    (comp.programming.threads)
  • Finding permutations of a list in a map or multimap
    ... I would like to use a map to store separate lists. ... Is there an easy way to do this with STL? ...
    (comp.lang.cpp)
  • Re: How to cast from list<T*> to list<const T*>
    ... > 1) You are working on a list why not use the STL list(or one of the ... > other containers)? ... I have my own functions for lists which are not present in standard ... You mean maybe to (const T*)? ...
    (comp.lang.cpp)
  • RE: data validation list: how do i force a user to enter data from t
    ... Each section of code begins with 4 Const definitions - change those to match ... Const countryColumn = "A" ' column with countries list ... 'assumes validation lists are in column B ... Set testRange = Worksheets. ...
    (microsoft.public.excel.misc)