CPB Mailing List

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: const keyword position



----------
> From: Tay Yong Haur <yhtay@mail.cc.utm.my>
> To: CPB Thread <cpb-thread@zdtips.com>
> Subject: const keyword position
> Date: Friday, March 06, 1998 9:13 PM
> 
> I am still confused with the position of the keywork : _const_ in a
> function as:
> 
> 	const int foo( int i );
> 
> 	and 
> 	
> 	int foo (int i) const; 
> 
> Can anyone explain to me! Thanks!


Don't fret.  *many* people don't understand this.

Let's take the first one first,
	const int foo( int i );
says that there is a function, "foo" which takes one argument, an integer,
and returns a constant integer.  This particular example is probably poorly
chosen, because there is no reason to return a constant integer, since the
return value is already itself a copy, and will not effect the underlying
object, but I digress.

	int foo (int i) const;
Now, this is obviously a class method, because regular functions can't be
declared this way.  For the sake of our discussion, let's assume it's a
method of class Foo.  With this assumption, it says that there is a
function foo that can be called on a constant Foo object.  foo() takes one
argument, an integer, and returns an integer.


Does this help?





W Komornicki's Home Page | Main Index | Thread Index