C++ getting round const

Started by
10 comments, last by Will F 17 years, 9 months ago
Quote:Original post by Fruny
Quote:Original post by ApochPiQ
This can also be done with aliasing, where multiple pointers/references point to a single object:

void SomeObject::ConstFunction(SomeObject& nonconstobj) const{   nonconstobj.member = value;}SomeObject foo;foo.ConstFunction(foo);


That's not really getting around anything, since you still need a non-const reference. The fact that you have a const member function, or even the fact that you have a member function at all is barely relevant. Except for accessing private members, I guess.

If I were to just give you a const Foo&, you'd be stuck.



This is true - but as long as a non-const Foo& is available somewhere, aliasing can still bypass constness to some extent. Yeah, the example's not so great, but I couldn't think of one that looks real-worldish and wasn't excessively long.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Advertisement
Quote:Original post by Anon Mike
Depending on what exactly they were asking for the keywords for you to research are "const_cast" and "mutable".


The C++ FAQ Lite's section on Const Correctness would be a good start.

This topic is closed to new replies.

Advertisement