22.13 The scope resolution operator and global functions

In C++, the ':: ' is called the 'scope resolution operator.' If you put a class name like MyClass in front of the operator, this indicates that you want to use the version of the class method implemented by the MyClass code. If, for instance, you have a ChildClass and a ParentClass, then ParentClass::Method() and ChildClass::Method() could very well mean different things.

Some functions are 'global' functions that aren't a member of any class at all. We try to make a practice of always putting two colons with no class name in front of a global function to remind ourselves that this is a function that doesn't belong to any class. Ordinarily, putting the ':: ' in front of a global function doesn't add anything or change anything; the code compiles and runs equally well with or without it. It's just something you do make your code easier to read. Thus, when I want to refer to the global Windows API method PlaySound, I'll normally write ::PlaySound.

The one time the ':: ' would really be necessary in front of a global function would be if you wrote a class method with the same name as a global function. If, for instance, you gave CPopView its own PlaySound method, then if you wanted to call the global PlaySound inside a CPopView method, you really would need to say ::PlaySound, as a call to PlaySound would call the CPopView::PlaySound method.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference