I found this issue has been one of those that made me open Google a lot. So I figured a summary in my blog might be great.

Used with Functions

const typename function_name(...) const ;

The first const applies to the returned type instance (this will not have any effect on built-in types). Thus, when it is used on any non-built-in types, the caller of the function is not suppose to or able to change returned value.

The latter one appears when this function is a member function of some object. It says that in this function, no member variables will be modified, unless those declared as mutable.

Used with Pointers

const typename * const var_name ;

The first one means that the pointer is pointing to a const variable of type typename, that is the thing it points to has a constant value and should not be changed. Note that it is the same if it is written as typename const* var_name.

The second one means that the pointer itself is immutable, that is, we cannot change what it points to.

Test Your Understanding!

Now, try to explain the following mad example. :)

const T* const function_name( const K* const ptr ) const ;


Leave a Comment