Immutable member functions on mutable objects

Tomek Sowiñski just at ask.me
Sun Nov 29 04:56:08 PST 2009


Tomek Sowiñski Wrote:

> Weird: if I reverse the situation -- the object is immutable and function is ordinary -- I also get an error.
> 
> struct A {
>     float _pole;
>     float pole() {
>         return _pole;
>     }
> }
> 
> void main() {
>     immutable A a;
>     auto x = a.pole;   // Ouch!
> }
> 
> Error: function hello.A.pole () is not callable using argument types ()

Sorry, scratch that. The compiler tripped correctly; pole() could've altered its object through "this" (the error message could be clearer, though).

So only my root post needs explaining.


More information about the Digitalmars-d-learn mailing list