Bypassing the const promise

Tomek Sowiñski tomeksowi.getridof at this.gmail.and.com.that
Thu Nov 19 10:48:39 PST 2009


const on a function forbids changing members:

class Wrong {
    int a;
    void foo() const {
        a = 4;
    }
}

The above rightly doesn't compile. But with a little twist...

class A {
    int a;
    void foo(ref int i) const {
        i = 4;
    }
    void foo() const {
        foo(a);
    }
}

void main() {
    auto a = new A;
    a.foo;
    assert(a.a == 4);
}

... I bypass the const promise on a function (two of them in fact). No casting, no evil stuff.

Is this a compiler bug or feature?

Tomek


More information about the Digitalmars-d-learn mailing list