Meaning of const

Jonathan M Davis jmdavisProg at gmx.com
Tue Jan 24 17:40:44 PST 2012


On Tuesday, January 24, 2012 16:24:27 Ali Çehreli wrote:
> On 01/24/2012 04:06 PM, Jonathan M Davis wrote:
>  >> class A {
>  >> int x;
>  >> 
>  >> const int f1() { ... }
>  >> int f2() const { ... }
>  >> const(int) f3() { ... }
>  >> }
> 
> [...]
> 
>  > int f2() const
>  > 
>  > becomes
>  > 
>  > int f2(const A this)
>  > 
>  > Now, the confusing part is the fact that unlike C++, D allows you to
> 
> put the
> 
>  > const for making the function on the _left-hand_ side of the function
> 
> (C++
> 
>  > only lets you put it on the right).
> 
> Ok.
> 
>  > So, while a C++ programmer expects that
>  > 
>  > int f2() const
>  > 
>  > means that f2 is const, they're likely to be surprised by the fact
> 
> that in
> 
>  > const int f1()
>  > 
>  > it's the int that's const, not f1.
> 
> Have you phrased that last bit correctly? :) It seems to conflict with
> what you have said earlier.
> 
> f1() and f2() are the same thing in D.

You're right. I said that incorrectly. f1 and f2 are identical.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list