Returning this from a const member

bearophile bearophileHUGS at lycos.com
Sat Mar 20 17:18:34 PDT 2010


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


More information about the Digitalmars-d-learn mailing list