Re: const question [C++]

From: BobR (RemoveBadBobR_at_worldnet.att.net)
Date: 03/23/05


Date: Tue, 22 Mar 2005 23:08:01 GMT


Mark P wrote in message ...
>Alf P. Steinbach wrote:
>>
>> I'd advice against that. Mark's description of the case means that
>> const iterator is correct, and making the set mutable would probably be
worse
>> than simply removing 'const' everywhere. The problems lie, as Mark
writes,
>> "elsewhere", and without any description of that no advice can be given.
>
>Fair enough, here's the gist of it. My const function is to return a
>pair of iterators representing a range in the set. This range is
>processed in a variety of ways, none of which affect the underlying set,
>and then eventually I want to erase the elements in the range from the
>set. There the const iterator becomes problematic.
>
>The problem as I understand it is that a const function won't let me
>return a non-const iterator (or a pair of the same) even though the
>function never attempts to modify the set via the iterator.
>
>So what's the recommended style to handle this? I could just eradicate
>the const declarations everywhere a conflict arises, but that's rather
>drastic. Is there a way to cast away the const'ness of an iterator?

const_cast<type>(const_something);

"Hey, you compiler! I'll take control of this one. Thanks for your help,
but, I know what I'm doing. I know it's 'const', you know it's 'const', it's
that stupid variable. It wants a non-const type, so, I need to fool it!!
Don't worry, I'll be sure not to do something stupid with it." <G>

Is that close to what you are looking for?

--
Bob R
POVrookie
--
MinGW (GNU compiler): http://www.mingw.org/
Dev-C++ IDE: http://www.bloodshed.net/
POVray: http://www.povray.org/
alt.comp.lang.learn.c-c++ faq:
   http://www.comeaucomputing.com/learn/faq/


Relevant Pages

  • Re: const question
    ... >> I'd advice against that. ... My const function is to ... There the const iterator ... it seems to me that if a member function is returning a non-const ...
    (alt.comp.lang.learn.c-cpp)
  • Re: const question
    ... > I'd advice against that. ... > than simply removing 'const' everywhere. ... There the const iterator becomes problematic. ... The problem as I understand it is that a const function won't let me ...
    (alt.comp.lang.learn.c-cpp)
  • Re: const question
    ... > 'const' everywhere. ... 'const' member functions to mutate *it*. ... [of an iterator which I don't think can be done]. ... > any description of that no advice can be given. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Multiple Constructors
    ... > class ListIterator { ... > insert before iterator ... > ListIterator endconst; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [Fixed PATCH] hpt366: fix section mismatch warnings
    ... Mark the pci table __devinitconst. ... A lot of variables are const but annotated __devinitdata. ... conflict error when build for 64 bit powerpc. ... The problem we face is that gcc in some cases mark the ...
    (Linux-Kernel)