const ending

Started by
5 comments, last by doynax 18 years, 11 months ago
Could any one remind me what does such function definition mean? Saw it somewhere and used also, but can't remind....8( //////////////////////////////////////////////////// int somufunc(int par1,float par2, etc...) const; //////////////////////////////////////////////////// THis case is not so clear as ordinar qualificators const, volatile,extern and others...
ID Software is waiting for me.Cleaners wanted there 8)
Advertisement
It's only applicable to member functions, it states that the method does not modify the state of the instance (except for mutable data members) its also the only type of member functions that constant instances can only invoke.

All accessor and query methods should be constant member functions regardless.
const double foo::wibble(const double bar) const {^^1^^                    ^^2^^             ^^3^^


const #1 claims that the returned value is constant.
const #2 claims that bar is constant.
const #3 claims that *this is constant. (Of course 'this' is constant; you can't just change which object you're working with in mid-stream.)
Quote:const #3 claims that *this is constant.

It changes this from a constant pointer to a constant pointer to constant data.
It's also worth noting that unlike return types the trailing const qualifier is part of the function's signature. And thus it's valid to overload a function in both a constant and non-constant form (probably returning constant and non-constant pointers), in which case the compiler prefers the non-constant version.
Quote:Original post by doynax
in which case the compiler prefers the non-constant version.


It's dependant on the context it's not a compiler preference

if you invoke a method inside another constant member function then the constant version is invoked.

if you have a constant reference to an instance or a pointer to constant data the constant version is invoked, etc, etc
Quote:Original post by snk_kid
Quote:Original post by doynax
in which case the compiler prefers the non-constant version.


It's dependant on the context it's not a compiler preference

if you invoke a method inside another constant member function then the constant version is invoked.

if you have a constant reference to an instance or a pointer to constant data the constant version is invoked, etc, etc
Eh, wasn't that pretty much what I said?
The compiler will choose the non-constant version if possible, and the constant otherwise. I.e. it prefers the non-constant version.

At least that's what I've always thought.

[Edited by - doynax on May 17, 2005 3:51:50 PM]

This topic is closed to new replies.

Advertisement