Chapter II Variables and basic types (8)

Source: Internet
Author: User

2.9 Writing your own header file

Objective

      1. To allow the program to be separated into separate logical blocks, C + + supports the so-called separate compilation
      2. The program can consist of multiple files

2.9.1 Design your own header file

      1. Header file provides a centralized location for related claims
      2. Header files typically contain
        1. Definition of Class
        2. Declaration of extern Variable
        3. of the function
      3. Use or define the file of the above entity, need to include header file
      4. How to compile and connect multiple source files
        1. g++-C main.cc Sales_item.cc-o Main
        2. Suppose just modified main.cc this source file, that also need to recompile sales_item.cc this file, much trouble
          1. The compiler provides a way to compile each file separately,
          2. Usually this process produces an. o file, and an extension of. o indicates that the file contains the target code
          3. The compiler allows us to connect the target file (. o) together to generate the executable file
          4. g++-C main.cc
          5. g++-C sales_item.cc
          6. g++ MAIN.O Sales_item.o-o Main
          7. Generated the Mian executable file

First, the header file is used for declaration, not for defining

      1. When designing a header file, it is important to remember the difference between the definition and the Declaration;
        1. Define only one occurrence
        2. Declarations can occur multiple times
      2. The following statements are definitions and should not be placed in the header file
        1. extern int ival = 10;
        2. Double fica_rate;
        3. If the ival variable is redefined in another file, it causes a multiple-definition connection error.
      3. Because the generic header file is contained in multiple source files, it should not contain the definition of a variable.
        1. Because in each source file contains the header file, in the actual compilation process, will use the real file content of the header file, to replace the # include "header file"
        2. If the definition of a variable is included in the header file, then in many source files, there is a duplicate definition of the variable when the content of the header file is replaced.
        3. However, there are some exceptions, which need to be noted here, as explained in the following:
          1. The class can be defined in the header file
          2. Value at compile time, you already know the Const object
          3. inline function

Second, some const objects are defined in the header file

    1. When the const variable defaults, it is the local space of the file that defines the variable
    2. A constant expression is a compiler that computes a result expression at compile time.
      1. Integer literal constants are constant expressions
    3. This const integer variable can be a constant expression when a const integer variable is self-initialized by a constant expression
      1. What do you mean
      2. For example, a const int c = 1;
      3. The const integer variable is initialized with a constant expression of 1, which can later be assumed to be a const variable of a constant expression
    4. Any variable in C + + can only be defined once, why?
      1. Because the storage space is allocated when the variable is defined
      2. All use of the variable is associated to the same storage space.
      3. int i = 5;
      4. A.cc used the I variable, b.cc used the I variable, c.cc used the I variable
      5. The use of all cc files on variables is associated with the same storage space.
    5. When a const variable is initialized with a constant expression, you can guarantee that all variables have the same value.
    6. In fact, in practice, most compilers replace the use of these const variables with corresponding constant expressions.
    7. So in practice, there is no actual storage space for const variables initialized with constant expressions
      1. const int i = 5;
      2. There is no real memory space in memory to store it
      3. Instead, replace the const int i variable with a constant expression 5来 when used.
    8. Initialization of a const variable
      1. Constant expression initialization, const int i = 5;
      2. const INT J = Func (A, b); For this case,
        1. Instead of initializing the const variable with a constant expression,
        2. This definition should not be placed in the header file,
        3. should be defined and initialized in the source file, just like any other variable.
        4. The extern declaration should be added to the header file so that it can be shared by multiple files

  

A brief introduction to the 2.9.2 preprocessor

Objective

      1. #include is part of the C + + preprocessor
      2. The source code of the preprocessor handler, which runs before the compiler
        1. Preprocessor---"Compiler---"
      3. In the source file that actually contains the header file, the preprocessor uses the contents of the header file instead of each # include
      4. We write our own header files stored in the file, the system header file may be stored in a more efficient way, such as binary??? It's just speculation.

First, header files often require other header files

      1. Header files often include other header files
      2. It is not uncommon for a header file to be include to the same source file multiple times
      3. When designing a header file, you should make the header file available for inclusion in the same source file multiple times.
      4. We must ensure that the classes and objects that contain the same header file multiple times will not cause the header definition to be defined more than once
      5. How to do the above guarantee? To define a header file protector using a preprocessor
        1. To avoid re-processing the contents of a header file if it has already been seen

Second, avoid multiple inclusions

      1. The preprocessor allows us to define our own variables
      2. Preprocessor variables are usually represented in all uppercase letters
      3. There are two states of preprocessor variables:
        1. has been defined
        2. Not defined
      4. #define接受一个名字, and define the name as the preprocessor variable
      5. #ifndef detects if the specified preprocessor variable is undefined, and if the preprocessor variable is undefined, then all instructions followed are processed, knowing that the #endif
      6. For example, you can use the following method to avoid including a header file multiple times
        1. #ifndef Salesitem_h
        2. #define Salesitem_h
        3. .... Defining the Sales_item Class
        4. #endif

Third, use the custom header file

      1. Header file in <>, that the header file is a standard header file, the compiler will find the header file in a predetermined location, the predetermined location can be set by setting the Find PATH environment variable or command line options to modify
      2. The header file is seen in "", the header file is considered to be non-system, and the lookup begins at the path where the source file resides.

Chapter II Variables and basic types (8)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.