Sign in to follow this  
Trillian

What are const functions?

Recommended Posts

Hello! I was wondering what is a constant function and why is it useful. And also if it must be used within a class or can be global/namespaced. exemple: class Foo //Why "Foo"? don't ask me { public: int Oof(char ofo) const; }; What are the advantages of such a function? when must it be used? Thanks!

Share this post


Link to post
Share on other sites
A const function is one that is guaranteed to never change the class it is called on. This allows you to catch potential bugs at compile time, which is always better than at runtime. So this wouldn't compile:


struct Foo
{
void Bar() const {foo_bar += 1;}

int foo_bar;
};


because function Bar() changes the value of a member of Foo. If the function was declared non-const, then it would compile.

There is no time when a const function must be used, but it allows you to prevent accidentaly changing something which shouldn't be, which can help avoid sneaky bugs that take hours to find. Const functions can only be members of a class/struct, since global/namespaced functions don't have any "parent" that they could change. However, you can declare arguments in any function to be const, which will prevent their modification. For instance, in this situation, it helps catch a nasty bug:



bool CheckValue(int & foo)
{

if (foo = 5) //ERROR! CHANGING A VALUE! SHOULDN'T BE! GLAD WE CAUGHT THAT!
return false;
if (foo == 0)
return true;
if (foo == 1)
return false;
return true;
}



It won't compile, because CheckValue changes the value of int foo, which will make this function always set foo to the value of 5 and return false. This can happen if you forget an equals sign, which even Carmack must do occasionally. Thank God for const functions!

Share this post


Link to post
Share on other sites
The const indicates that the function won't modify any1 class members. It is extremely important, as the following case shows:

class Foo
{
public:
int HiddenConst() {
return i;
}

int ExplicitConst() const {
return i*i;
}

// Compile Error ahead: const function modified i, that isn't allowed
// void NotConst() const {
// i = 5;
//}

private:
int i;
};

int main()
{
const Foo bar;

bar.HiddenConst(); //compile error - HiddenConst may change bar, but bar is const and thus can't be changed
bar.ExplicitConst(); //OK - ExplicitConst is known to not change bar, so calling it on const objects is all right
}



In this trivial example, the compiler could probably tell that HiddenConst() should actually be a const function and treat it as such, but in general it can't. So the only safe way to handle the situation is for the programmer to tell it which functions are safe and which aren't.

CM

1 Technically, you can specify members as mutable, in which case they can be changed no matter what. But they're rare. *edit: Fixed slight bug...volatile is a completely different beast from mutable...thanks sordid

Share this post


Link to post
Share on other sites
However, member variables _can_ be changed within a const function if the variables are defined 'mutable'.

ie.


class Foo
{
public:
void bar(int x) const
{
mX = x;
}
private:
mutable int mX;
};




The first massive usage of this I ever noticed was in the Ogre3d engine.

This is so that even if a function is defined const, or the instance pointer is const and 'bar' is called from it, you can tell exactly what member variables can be changed. Atleast, this is _my_ personal explanation for it ;)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Conner McCloud
The const indicates that the function won't modify any1 class members.

Should read: The const indicates that the function won't modify any1 non-static class members.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this