Returning this from a const member

Robert Clipsham robert at octarineparrot.com
Sat Mar 20 17:26:44 PDT 2010


On 21/03/10 00:18, bearophile wrote:
> Robert Clipsham:
>> Why is this?
>
> It's not a hard question. This code compiles:
>
> class Foo {
>      const(Foo) myFoo() const {
>          return this;
>      }
> }
> void main() {
>      auto f = new Foo;
> }
>
> It's just in a const function the "this" is const, so if you want to return it, then you have to use a "const Foo" type, because it's not a mutable Foo :-)
>
> Bye,
> bearophile

Now you say it, that does seem obvious, thank you :)


More information about the Digitalmars-d-learn mailing list