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


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

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: mutable vs const_cast, which is better to modify field from const function?
Date Sat, 09 Jul 2011 20:04:14 +1200
Lines 46
Message-ID <97qgdgF6tmU3@mid.individual.net> (permalink)
References <iv8s22$2sp$1@speranza.aioe.org> <97qea4F6tmU2@mid.individual.net> <iv91p9$fjl$2@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net InwSQ9YPkvW16qKWrsmI2QA8T2u0WF6jMTnFkJ4F0WF3iPXffh
Cancel-Lock sha1:IJ8risCmx7mW+co0n+6ghIj/fGM=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <iv91p9$fjl$2@speranza.aioe.org>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:7671

Show key headers only | View raw


On 07/ 9/11 07:56 PM, Qi wrote:
> On 2011-7-9 15:28, Ian Collins wrote:
>
>> How about:
>>
>> struct SomeClass
>> {
>> SomeClass() {}
>>
>> MyObject* getMyObject() const
>> {
>> static MyObject* myObject;
>>
>> if(!myObject)
>> {
>> myObject = new MyObject;
>> }
>>
>> return myObject;
>> }
>> };
>
> That's another method.
> But I do want myObject be member data so other functions
> may check "myObject == NULL" when initialization is not
> required.

struct SomeClass
{
   SomeClass() {}

   MyObject* getMyObject( bool initialise = false ) const
   {
     static MyObject* myObject = NULL;

     if( !myObject && initialise )
     {
       myObject = new MyObject;
     }
		
     return myObject;
   }
};

-- 
Ian Collins

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