www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Meaning of const

Hi all,

I'm a bit confused by what 'const' means when used as part of a member
function's signature:

	class A {
		int x;

		const int f1() { ... }
		int f2() const { ... }
		const(int) f3() { ... }
	}

Is there a difference between f1 and f2? If not, which is the preferred
syntax? If yes, what is the meaning of 'const' in either case?

I tried various code inside f1 and f2, and it seems in both of them I
can't change x, which appears to mean that 'const' in this case refers
to immutability of the class object? Is this correct?

What exactly does 'const' mean in f3? That the returned int cannot be
modified?

Confused by const syntax,


T

-- 
English has the lovely word "defenestrate", meaning "to execute by
throwing someone out a window", or more recently "to remove Windows from
a computer and replace it with something useful". :-) -- John Cowan
Jan 24 2012