Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7665
| From | Qi <no@no.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | mutable vs const_cast, which is better to modify field from const function? |
| Date | 2011-07-09 14:19 +0800 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <iv8s22$2sp$1@speranza.aioe.org> (permalink) |
It's not rare to lazy initialize member field in getter functions which
are const function.
class SomeClass {
public:
MyObject * getMyObject() const {
if(! this->myObject) {
this->myObject = create the object;
}
return this->myObject;
}
private:
MyObject * myObject;
};
To be able to modify myObject in the getter function, we can
either,
1, Use mutable on myObject.
mutable MyObject * myObject;
or
2, Use const_cast to cast away constness from "this".
const_cast<SomeClass *>(this)->myObject = create the object;
Both methods have their own pros and cons, that's why I can't
decide which is better.
Method 1 (mutable),
Pros:
It's quite nature. Since myObject can be modified by a
const function, it's natural that it's mutable.
Cons:
A, "mutable" makes myObject to be changable to all const
functions. This is the problem. It should not be able to be
modified by any const functions except the lazy initialization
(the getter).
B, Any one reading the code may misunderstand that myObject
is really mutable while it's not.
Method 2 (const_cast),
Pros:
The effect is only limited to the getter function, any other
const functions can't modify myObject.
Cons:
A, Casting a const "this" to non-const looks so ugly and not
natural.
B, And a const function being able to modify a non-mutable member
is not natural too.
My questions:
1, Which method do you prefer to implement lazy initialization?
2, I think both methods are quite standard C++ (03), right?
If not, please point out.
Thanks
--
WQ
Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread
mutable vs const_cast, which is better to modify field from const function? Qi <no@no.com> - 2011-07-09 14:19 +0800
Re: mutable vs const_cast, which is better to modify field from const function? Narinder <narinder.claire@gmail.com> - 2011-07-09 00:20 -0700
Re: mutable vs const_cast, which is better to modify field from const function? Qi <no@no.com> - 2011-07-09 15:50 +0800
Re: mutable vs const_cast, which is better to modify field from const function? Narinder <narinder.claire@gmail.com> - 2011-07-09 01:36 -0700
Re: mutable vs const_cast, which is better to modify field from const function? Ian Collins <ian-news@hotmail.com> - 2011-07-09 19:28 +1200
Re: mutable vs const_cast, which is better to modify field from const function? Qi <no@no.com> - 2011-07-09 15:56 +0800
Re: mutable vs const_cast, which is better to modify field from const function? Ian Collins <ian-news@hotmail.com> - 2011-07-09 20:04 +1200
Re: mutable vs const_cast, which is better to modify field from const function? Narinder <narinder.claire@gmail.com> - 2011-07-12 15:41 -0700
Re: mutable vs const_cast, which is better to modify field from const function? SG <s.gesemann@gmail.com> - 2011-07-09 01:25 -0700
Re: mutable vs const_cast, which is better to modify field from const function? Werner Erasmus <werasm@gmail.com> - 2011-07-10 22:25 +0200
Re: mutable vs const_cast, which is better to modify field from const function? Qi <no@no.no> - 2011-07-11 09:54 +0800
Re: mutable vs const_cast, which is better to modify field from const function? Werner Erasmus <werasm@gmail.com> - 2011-07-11 09:08 +0200
Re: mutable vs const_cast, which is better to modify field from const function? Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-09 03:31 -0500
Re: mutable vs const_cast, which is better to modify field from const function? "Balog Pal" <pasa@lib.hu> - 2011-07-09 10:47 +0200
Re: mutable vs const_cast, which is better to modify field from const function? Qi <no@no.com> - 2011-07-09 16:54 +0800
csiph-web