The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.

Oddities and Errors

If you have both member functions and nonmember functions in the same class definition, it is easy to get confused. A common way to organize a class definition is to put all the constructors at the beginning, followed by all the member functions and then all the nonmember functions.

You can have a member function and a nonmember function with the same name, as long as they do not have the same number and types of parameters. As with other kinds of overloading, C++ decides which version to invoke by looking at the arguments you provide.

Since there is no current object in a nonmember function, it is an error to use the keyword this. If you try, you might get an error message like: "Undefined variable: this." Also, you cannot refer to instance variables without using dot notation and providing an object name. If you try, you might get "Can't make a static reference to nonstatic variable..." This is not one of the better error messages, since it uses some non-standard language. For example, by "nonstatic variable" it means "instance variable." But once you know what it means, you know what it means.

Last Update: 2005-11-21