const member function

rumbu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 21 00:27:13 PST 2015


On Saturday, 21 February 2015 at 08:08:25 UTC, Mike Parker wrote:
> On 2/21/2015 4:31 PM, rumbu wrote:
>
> you can do this instead:
>
>     this() { cache = SomeExpensiveOp(); }
>
>     public @property const(int) SomeProp() const
>     {
>         return cache;
>     }
>
> Notice the const on the end of SomeProp. That makes the 
> function callable on a const instance, but you still cannot 
> modify cache inside of it.

My question was not how I do this, I know already. My question 
was if there is another way to safely call a non-const instance 
function on a const object.

Initializing "cache" in the constructor will defeat the cache 
mechanism itself an that's I want to avoid.


More information about the Digitalmars-d-learn mailing list