Meaning of const

H. S. Teoh hsteoh at
Tue Jan 24 15:52:50 PST 2012

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

Confused by const syntax,


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

More information about the Digitalmars-d-learn mailing list