Thursday, December 13, 2007

static and const member functions

A const member function ensures at compiletime that this member function does not change an instance of its class.
A static member function does not has the dynamic aspects of an instance of the class because it has no this pointer. So it has the same behaviour for all objects (instances).

When designing a new class you have to provide a set of member functions this class has. It is so called good style to set all member functions which do not "need" the this pointer to be static functions. All non-static member functions have to be const or non-const. So when a function does not change the object make it a const function! Keep in mind that const functions are only allowed to call other const functions of that class, otherwise your compiler will grumble!

Not using const and static is bad style!