I have two quick questions:
1) What is the point of a static method? As far as I can tell, there is no data there that would be accessed by multiple objects as there is no data stored there at all...
2)What does it mean to have a const after the name of function, like this:

void function::function2() const

What is the last constant doing?

A static method can only access static members of the class. This feature doesn't prove useful very often, in my experience.

The "const" at the end of a function bans the function from modifying any members of the object.

>>What is the point of a static method?

The point of a static method is that it can access all the private members of the class but isn't tied to an object (instance of the class). I would agree that it isn't the most useful thing in the world. One typical use is the factory function pattern:

class Foo {
  private:
    Foo();  //private constructor.
  public:
    static Foo* Create() {
      return new Foo();  //can access the private constructor.
    };
};

The above idiom is useful if you want to enforce a certain means of creating the objects of a class, or if you want to inject some post-construction code, or furthermore, this idiom can be used to give names to the different constructors (e.g. "FromString()"). This factory function idiom is very wide-spread, and the syntax resulting from using a static function is very neat too, e.g., Foo* pobj = Foo::Create(); (but note also that "factory patterns" can refer to many variations that may or may not involve a static function). The singleton design pattern is another idiom that usually involves a static method for the same reason as above, to make the constructor private.

Another general purpose for static functions is to provide functions that are related to the class but don't need to manipulate an object of that class. However, do not confuse static methods with friend functions, which are typically more useful than static methods to add related functionality to a class.

Static functions also become useful in more advanced programming techniques because you can attach a function to a class, but not to an object. This can be very useful when working with templates in C++.


>>What is the last constant doing?

The last "const" means that the object on which the member function is called is not permitted to be modified (i.e. the object is const). You have to understand that a member function is called as if it had a hidden parameter given to it, that hidden parameter is the "this" pointer which points to the object on which the member function is called. In other words, you have this:

class foo {
  public:
    void bar_mem();
    void bar_mem_const() const;
};

void bar_free( foo* this );
void bar_free_const( const foo* this );

int main() {
  foo obj;
  //these two calls are essentially equivalent:
  obj.bar_mem();
  bar_free(&obj);
  //and these two calls are also equivalent:
  obj.bar_mem_const();     //cannot modify "obj"
  bar_free_const(&obj);    //cannot modify "obj"
};

Thanks a lot everyone. That really helped me out.
@Mike
Regarding your terminology, what is an idiom in this context, and why is it called a factory function pattern? Not the most important things, I know...

>>what is an idiom in this context

A programming idiom is essentially a code structure (or sub-structure, or pattern) that is often used by programmers. Because there are recurrent problems in programming (like iterating through an array, controlling the creation method of objects, performing conversions, encapsulating resources, etc.), there are recurring code structures that solve those problems, and we call those idioms or design patterns. You can check out the wiki on Programming Idioms and here is a pretty complete list for C++. You can also check the wiki on design patterns.

This article has been dead for over six months. Start a new discussion instead.