Wrox Press C++ Tutorial
Summary
You now have a reasonably comprehensive knowledge of writing and using functions. You've used a pointer to a function, learnt about exceptions and you have used overloading to implement a set of functions providing the same operation with different types of parameters. We'll see more about overloading functions in the following chapters. We've also looked at function templates, working with program files and namespaces
The important bits that you learned in this chapter are:
-
A pointer to a function stores the address of a function, plus information about the number and types of parameters and return type for a function.
-
You can use a pointer to a function to store the address of any function with the appropriate return type, and number and types of parameters.
-
You can use a pointer to a function to call the function at the address it contains. You can also pass a pointer to a function as a function argument.
-
Overloaded functions are functions with the same name, but with different parameter lists.
-
When you call an overloaded function, the function to be called is selected by the compiler based on the number and types of the arguments that you specify.
-
A function template is a recipe for generating overloaded functions automatically.
-
A function template has one or more arguments that are type variables. An instance of the function template - that is, a function definition - will be created by the compiler for each function call that corresponds to a unique set of type arguments for the template.
-
You can force the compiler to create a particular instance from a function template by specifying the function you want in a prototype declaration.
-
Exceptions help separate error-handling code from your main program logic, and can provide specific information on the cause of the problem.
-
C++ programs consist of header and source files. When the code is compiled, the relevant header files are called into the source code files, and each source code file is compiled to an object file. These object files are then linked to form a single executable.
-
In order to reuse variables between source code files, you must make use of the
extern keyword.
-
Namespaces are a way to avoid clashes between non-unique variable and function names in C++. You specify a namespace to which your code belongs, which is then used to qualify the name.
-
To refer to code that belongs to a name space, you can use using directives and declarations, or refer to it directly using the scope resolution operator.
© 1998 Wrox Press