Prototype

Method prototypes are like Virtual functions in that they are declared in a class or interface, and are overridden by other code. A prototype is missing the method body, and has a semicolon after it to end the line.

is an example of a prototype.

To make a prototype functional, you must declare the override with a method body.

Prototypes are normally used to make a method aware of another method before its declaration. For example, in windows programming, the WndProc method is normally declared after the WinMain method. The problem with this is that the WinMain method needs access to WndProc, but the access cannot be granted because WinMain comes earlier in the code than WndProc does. To solve this problem, use a prototype.

The above code will generate an error, because function1 tries to call function2, but the program is not yet aware of its existance.

This code will work. Notice that the prototype doesn't name the variable q. Arguments declared in prototypes do not necessarily need the same names as the arguments in the override. In fact, they don't need names at all, as long as they have the same variable types (int, float, etc).

Prototypes are a very fundamental code concept, and are used extensively in classes. Here is an example of a prototype applied in this manner.

contents of PrototypeSample.h:

contents of PrototypeSample.cpp:

See how the function prototypes are first declared in the H file, then overridden in the cpp file?