- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 650字
- 2021-06-10 18:28:03
Declaring and defining functions
A function must be defined exactly once, but through overloading, you can have many functions with the same name that differ by their parameters. Code that uses a function has to have access to the name of the function, and so it needs to have access to either the function definition (for example, the function is defined earlier in the source file) or the declaration of the function (also called the function prototype). The compiler uses the prototype to type-check that the calling code is calling the function, using the right types.
Typically, libraries are implemented as separate compiled library files and prototypes of the library functions are provided in header files so that many source files can use the functions by including the headers. However, if you know the function name, parameters, and return type, you can type the prototype yourself in your file.
Whichever you do, you are simply providing the information for the compiler to type-check the expression that calls function. It is up to the linker to locate the function in the library and either copy the code into the executable or set up the infrastructure to use the function from a shared library. Including the header file for a library does not mean that you will be able to use the functions from that library because in standard C++, the header file does not have information about the library that contains a function.
Visual C++ provides a pragma called comment, which can be used with the lib option as a message to the linker to link with a specific library. So #pragma comment(lib, "mylib") in a header file will tell the linker to link with mylib.lib. In general, it is better to use project management tools, such as nmake or MSBuild, to ensure that the right libraries are linked in the project.
Most of the C Runtime Library is implemented this way: the function is compiled in a static library or a dynamic link library, and the function prototypes are provided in a header file. You provide the library in the linker command line, and typically you will include the header file for the library so that the function prototypes are available to the compiler. As long as the linker knows about the library, you can type the prototype in your code (and describe it as external linkage so the compiler knows the function is defined elsewhere). This can save you from including some large files into your source files, files that will mostly have prototypes of functions that you will not use.
However, much of the C++ Standard Library is implemented in header files, which means that these files can be quite large. You can save compile time by including these header files in a precompiled header.
So far in this book, we have used one source file so all the functions are defined in the same file as where they are used, and we have defined the function before calling it, that is, the function is defined above the code that calls it. You do not have to define the function before it is used as long as the function prototype is defined before the function is called:
int mult(int, int);
int main()
{
cout << mult(6, 7) << endl;
return 0;
}
int mult(int lhs, int rhs)
{
return lhs * rhs;
}
The mult function is defined after the main function, but this code will compile because the prototype is given before the main function. This is called a forward declaration. The prototype does not have to have the parameter names. This is because the compiler only needs to know the types of the parameters, not their names. However, since parameter names should be self-documenting, it is usually a good idea to give the parameter names so that you can see the purpose of the function.