Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7669

Re: mutable vs const_cast, which is better to modify field from const function?

From Qi <no@no.com>
Newsgroups comp.lang.c++
Subject Re: mutable vs const_cast, which is better to modify field from const function?
Date 2011-07-09 15:50 +0800
Organization Aioe.org NNTP Server
Message-ID <iv91dr$fjl$1@speranza.aioe.org> (permalink)
References <iv8s22$2sp$1@speranza.aioe.org> <0a3d5245-a6d6-4b11-bf09-ae3f80333e44@h8g2000yqj.googlegroups.com>

Show all headers | View raw


On 2011-7-9 15:20, Narinder wrote:

> struct MyObject{};
>
> struct SomeClass
> {
>      SomeClass():myObject(0){}
>
>      const MyObject * getMyObject()const
>      {
>          return getMyObject_impl();
>      }
>
>      MyObject * getMyObject()
>      {
>          return getMyObject_impl();
>      }
>
> private:
>
>      const MyObject * getMyObject_impl()const
>      {
>          if(!myObject)
>          {
>              myObject = new MyObject();
>
>          }
>          return myObject;
>      }
>
>      MyObject * getMyObject_impl()
>      {
>          if(!myObject)
>          {
>              myObject = new MyObject();
>
>          }
>          return myObject;
>      }
>
>      mutable MyObject * myObject;
> };
>
> -------------------------------------------------
>
>
> So as long as all your other member functions access myObject via the
> getMyObject_impl methods it should be ok.

First, sorry that I forgot the "const" in the return of the getter.
I did mean const MyObject * getMyObject()const.

Back to my question, seems you are suggesting to use "mutable", right?


-- 
WQ

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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