2.9.2 A Brief Introduction to the Preprocessor (1)

2013-10-07 15:22:47 · 作者: · 浏览: 68

Now that we know what we want to put in our headers, our next problem is to actually write a header. We know that to use a header we have to #include it in our source file. In order to write our own headers, we need to understand a bit more about how a #include directive works. The #include facility is a part of the C++(www.cppentry.com) preprocessor. The preprocessor manipulates the source text of our programs and runs before the compiler. C++(www.cppentry.com) inherits a fairly elaborate preprocessor from C. Modern C++(www.cppentry.com) programs use the preprocessor in a very restricted fashion.

EXERCISES SECTION 2.9.1

Exercise 2.31: Identify which of the following statements are declarations and which ones are definitions. Explain why they are declarations or definitions.

(a) extern int ix = 1024;

(b) int iy;

(c) extern int iz;

(d) extern const int &ri;

Exercise 2.32: Which of the following declarations and definitions would you put in a header In a source file Explain why.

(a) int var;

(b) const double pi = 3.1416;

(c) extern int total = 255;

(d) const double sq2 = sqrt(2.0);

Exercise 2.33: Determine what options your compiler offers for increasing the warning level. Recompile selected earlier programs using this option to see whether additional problems are reported.

A #include directive takes a single argument: the name of a header. The preprocessor replaces each #include by the contents of the specified header. Our own headers are stored in files. System headers may be stored in a compilerspecific format that is more efficient. Regardless of the form in which a header is stored, it ordinarily contains class definitions and declarations of the variables and functions needed to support separate compilation.