Re: using map, list etc. in const methods

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


Date: Fri, 23 Jan 2004 20:51:14 +0100

Donovan Rebbechi wrote:

> 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
thanks, that makes sense :)

> btw, you can do this using
>
> count_if(themap.begin(),themap.end(),isWhite); // isWhite returns true if the piece is white
very handy. I'll use it.

>
>>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 ?
In this case the board creates the nodes, stores them in the map and
deletes them in it's destructor. I see that ownership is a question of
design. Thanks!

>>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
I'll take a look on Josuttis book.

Another question:
If I had a public method getNodeMap() that should return my nodes-map to
the caller, is there a possibility to make this method const and to
return a const version of the map, so that the caller may not change the
returned map? I tried that once, but failed because of a lot of compiler
errors.

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


Relevant Pages

  • Re: Writing single bits to a file
    ... The 'const' keyword provides the same kind of benefit that prototypes ... enabling the compiler to warn you if it detects the fact that you ... I had not gone and more correctly fixed up the precedence heirarchy (unary ... I like to implement scripting languages for breakfast ...
    (comp.lang.c)
  • Re: const
    ... > const modifiers, ... willing to remove the const'ness with a cast, ... I don't know how good a cross-file compiler could be - it would have to be ... it's a guarantee by the programmer. ...
    (comp.programming)
  • Re: Index a #define string
    ... In this case, i wouldn't use an array myself but a const char * const, but ... should one make it of static storage? ... If they were on the stack, where do you think their values ... You mean that the compiler optimises it out. ...
    (comp.lang.cpp)
  • Re: How to define a const and initialize it?
    ... > do the same work as const int MYCONST will do.... ... But the thing is that macro substitution takes ... C++ compiler isn't even aware of its existence. ... This Item might better be called "prefer the compiler to the preprocessor," because #define is often ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Most Economical VB Development Compiler
    ... You can only define a conditional compiler constant to have one value. ... Attempting to do so produces a "Compile Error: ...
    (microsoft.public.vb.winapi)