Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const correctness - should C++ prefer const member over non-const?

Reply
Thread Tools

const correctness - should C++ prefer const member over non-const?

 
 
Martin Eisenberg
Guest
Posts: n/a
 
      10-30-2008
fungus wrote:

> Bummer. I've got an object which triggers quite
> a big internal rebuild when you call the non-const
> version and I just noticed it's doing a lot of
> rebuilding because of this assumption.


Then either the two methods' semantics are too different for a
single identifier, or the entity making the offending calls shouldn't
get its hands on a non-const reference, or you should memoize the
rebuilding method.


Martin

--
Quidquid latine scriptum est, altum videtur.
 
Reply With Quote
 
 
 
 
fungus
Guest
Posts: n/a
 
      10-30-2008
On Oct 30, 1:50*pm, Martin Eisenberg <(E-Mail Removed)> wrote:
> the entity making the offending calls shouldn't
> get its hands on a non-const reference
>


This is probably the best solution. I need both const
and non const access inside an event handler function
so I guess I need to make the access more fine-grained.


--
<\___/>
/ O O \
\_____/ FTB.

http://www.topaz3d.com/ - New 3D editor!
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-30-2008
On Oct 30, 1:15 pm, fungus <(E-Mail Removed)> wrote:
> On Oct 30, 11:45 am, (E-Mail Removed) (blargg) wrote:


> > As others have mentioned, a proxy object with an operator T
> > () and operator = ( T const& ) would be most transparent to
> > the user.


> I don't see how that works for a struct...


It doesn't, really.

> I want operator[] to return a reference to a struct and be
> able to do this:


> Foo f = myStuff[i].foo;


> and


> myStuff[i].foo = f;


> With the first one calling the const version of operator[].


There are several solutions. The best would be to find a
compiler modified to allow you to overload operator., and use
that. But that sort of ruins portability. Otherwise, you can
overload operator-> in the proxy, and tell your users to use
that whenever they do a []---it's ugly and counter-intuitive,
but it works. And for special cases, you can define all of the
members in the proxy, so that they forward to the correct member
of owning class. Or you can just require your clients to get a
reference first, e.g.:
Whatever const& rf = myStuff[ i ].foo
Foo f = rf.foo ;

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
fungus
Guest
Posts: n/a
 
      10-31-2008
On Oct 31, 4:46*am, (E-Mail Removed) (blargg) wrote:
>
> * * class Stuff {
> * * public:
> * * * * Whatever const& operator [] ( int ) const;
> * * * * Whatever& modify( int );
> * * };
>
> * * void user( Stuff& myStuff )
> * * {
> * * * * Foo f = myStuff[ i ].foo;
> * * * * // ...
> * * * * myStuff.modify( i ).foo = f;
> * * }
>


This (or variations on it) works too...


--
<\___/>
/ O O \
\_____/ FTB.

http://www.topaz3d.com/ - New 3D editor!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
should we prefer deque over vector yancheng.cheok@gmail.com C++ 5 09-14-2006 01:23 PM
Const correctness for pointer data members quantdev2004@yahoo.co.uk C++ 10 10-05-2005 12:09 PM
const-correctness and lambda expression Matthias Kaeppler C++ 1 04-20-2005 10:09 PM
stl list, const correctness Jim Strathmeyer C++ 2 03-20-2005 12:37 AM



Advertisments