www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/12/05/02:35:14

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: Alex Vinokur <alexvn AT bigfoot DOT com>
Newsgroups: comp.os.msdos.djgpp,comp.lang.c++,alt.comp.lang.learn.c-c++
Subject: Re: Passing 'const' as 'this' discards qualifiers
Date: Wed, 05 Dec 2001 09:15:19 +0200
Organization: Scopus Network Technologies
Lines: 95
Message-ID: <3C0DC987.39C13400@bigfoot.com>
References: <3C0CCEE3 DOT A2D60376 AT bigfoot DOT com> <3C0CD55F DOT B04E8089 AT gascad DOT at> <3C0CD695 DOT 7F408020 AT bigfoot DOT com> <3C0CF679 DOT D0A24FB8 AT gascad DOT at> <3C0DB9D0 DOT 6217E7AD AT bigfoot DOT com> <WgjP7.548$cH6 DOT 58104 AT rwcrnsc52>
NNTP-Posting-Host: 62.90.123.5
Mime-Version: 1.0
X-Trace: fu-berlin.de 1007536522 9190557 62.90.123.5 (16 [79865])
X-Mailer: Mozilla 4.7 [en] (Win98; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com


Victor Bazarov wrote:

[snip]

> My guess would be that since 'set' is an associative container,
> dereferencing an iterator returns a constant object (each set
> is an ordered collection, what good would non-const object ref
> do if you could arbitrarily change it?)
>
> >   2. What to do if foo2 must be non const?
>
> Tough luck.  You are not supposed to change the object once they
> have been inserted into an associative container.  That would
> potentially change their relation and violate their ordering.
>
> Victor
> --
> Please remove capital A's from my address when replying by mail

Thanks. You are right.

Here is convincing example using map.
We can see that
  * there is no problem with second.foo2();   // foo2() is non const
  * there is a problem with first.foo2();

===============================================================
Windows98
gpp : GNU C++ version 2.95.3 20010315/djgpp (release) (djgpp)
      compiled by GNU C version 2.95.3 20010315/djgpp (release).
===============================================================


//------------- C++ Code : BEGIN -------------

#include <set>
#include <map>

class AAA;

class less_AAA
{
public:
  bool operator()(AAA& ins1, AAA& ins2) {return false;}
};

class AAA
{
  public:
    void foo1 () const {};
    void foo2 () {};
};

int main ()
{
map<AAA, AAA, less_AAA>::iterator iter_map;
  iter_map->second.foo1();
  iter_map->second.foo2();

  iter_map->first.foo1();
  iter_map->first.foo2(); // Line#26

set<AAA, less_AAA>::iterator iter_set;
  iter_set->foo1();
  iter_set->foo2();  // Line#30

  return 0;
}


//-------------- C++ Code : END --------------

//------------- Compiling : BEGIN ------------

%gpp ttt.c

ttt.c: In function `int main()':
ttt.c:26: passing `const AAA' as `this' argument of `void AAA::foo2()' discards
qualifiers
ttt.c:30: passing `const AAA' as `this' argument of `void AAA::foo2()' discards
qualifiers

//-------------- Compiling : END -------------

  ===========================
  Alex Vinokur
    mailto:alexvn AT bigfoot DOT com
    mailto:alexvn AT dr DOT com
    http://up.to/alexvn
    http://go.to/alexv_math
  ===========================


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019