Bypassing the const promise
    div0 
    div0 at users.sourceforge.net
       
    Thu Nov 19 12:01:02 PST 2009
    
    
  
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Tomek Sowiñski wrote:
> 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
That's definitely a bug.
Casting away const should always be explicit operation.
- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iD8DBQFLBaP+T9LetA9XoXwRAjsWAJ4g8F2vShGWRsiOXiqa2ONBr4YMvACeM+UX
/M2U4uArNNu3Xb6vGgKoIKc=
=+WE+
-----END PGP SIGNATURE-----
    
    
More information about the Digitalmars-d-learn
mailing list