/*Orion Lawlor's Simple C++ Examples, olawlor@acm.org
Shows the semantics of "const" member functions.
A "const" member function does not change the object,
and hence can be called on a const object.
*/

class foo {
	int i;
public:
	int bar(void) 
	{
		i=1;
	}
	int cbar(void) const /* means "this" is const here */
	{
		/* cannot modify i here, because of the "const" */
		return i;
	}
};

int main() 
{
	foo f;
	
	foo &r=f; /* non-const reference to f */
	r.cbar(); /* can call const member */
	r.bar(); /* can call non-const member */
	
	const foo &c=f; /* const reference to f */
	c.cbar(); /* can call const member */
	/* Cannot call non-const member via a const reference: */
	// c.bar(); // Error: passing `const foo' as `this' argument of `int foo::bar()' discards qualifiers
}
/*<@>
<@> ******** Program output: ********
<@> */
