const and immutable

Steven Schveighoffer schveiguy at yahoo.com
Tue Jul 6 11:06:38 PDT 2010


On Tue, 06 Jul 2010 12:10:04 -0400, Tim Verweij <tjverweij at gmail.com>  
wrote:

> Not sure if I'm double posting now. Sorry if I am, but I didn't see my  
> own post
> appear this time.
>
> Anyway, thanks for your replies, that was very helpful. I had one more  
> question
> about inout. If I understand correctly it cannot be used to get rid of  
> the double
> GetBar function in the following C++ example:
>
> class Foo
> {
> public:
>   const Bar& GetBar() { return mBar; } const
>         Bar& GetBar() { return mBar; }
> private:
>   Bar mBar;
> };
>
> Is that correct? Or is there a way to make this one function in D?

I'm not positive, but I think the second const applies to the second  
function, I think you meant:

const Bar& GetBar() const { return mBar; }

And yes, inout will reduce this to one function:

ref inout(Bar) GetBar() inout { return mBar; }

In addition, you do not have to specify the immutable version, therefore  
it actually saves 2 functions.

That is why it's there :)

-Steve


More information about the Digitalmars-d-learn mailing list