What are const functions?

Started by
8 comments, last by Mercury 18 years, 5 months ago
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!
Advertisement
The const in this case, means that the function will not modify anything in the class. When you create a const object from that class, you can only call const functions.
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!
my siteGenius is 1% inspiration and 99% perspiration
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
OK alright thanks I get it!

I'm gonna start using them right now!
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 ;)
Lots of great info in the C++ FAQ Lite. Here's a link to section [18.10] What is a "const member function"?
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.

Is there a difference between using:
int Oof(char ofo) const;

and:
const int Oof(char ofo);

?
Quote:const int Oof(char ofo);

This returns a constant integer.

Quote:int Oof(char ofo) const;

This is a constant function.

This topic is closed to new replies.

Advertisement