2-6 C/C + + writing header files

It is recommended to read the summary directly. If you don't understand anything, look back at the details

How does the header file work

When a test When #include "Sal_Item.h" in CPP, the compiler will actually_ Item. Copy all contents in the H file to test In CPP

//Sale_Item.h [incomplete version]
#include<string>
struct Sale_Item{
    std::string bookname;
    double renenue;
    int copies;
};
//test.cpp
#include<iostream>
#include<string>
#include"Sale_Item.h"  //Sale_ Item. The contents of h will be copied here
using namespace std;
int main(){
    Sale_Item data;
    return 0;
}

Avoid repeated references to header files

  • Because the header file is Sale_Item.h content will be copied to the file test. H that references it In CPP

  • So if the header file is sale_ Item. If h is repeatedly referenced, it will result in sales_ Item. The variables defined in H are in test CPP is defined multiple times, causing compilation errors.

  • Therefore, it is necessary to avoid repeated references to header files and global variables with the same name defined in different header files [when these files with variables with the same name are #include to the same program, compilation errors will also be caused]

  • Sales above must be_ Item. H make improvements, otherwise an error will be reported once it is repeatedly quoted

    Execute the following code

    #include<iostream>
    #include<string>
    #include"Sale_Item.h"
    #Include "sales_item. H" / / sales is called repeatedly_ Item. h. For the header file sales at this time_ Item. H is not allowed
    using namespace std;
    int main(){
        Sale_Item data;
        std::string name = "xxx";
        data.bookname = name;
        return 0;
    }
    

    result

Method to avoid repeated references to header files: conditional compilation

1. Add a preprocessor variable to each header file as a Label

#define SALE_ITEM_H

  • At this time, define is used to define the precompiled variable SALE_ITEM_H. Instead of defining macros
  • Why are precompiled variables so named?
    • The contents of the header file will be copied to test CPP, and the precompiled variables ignore the scope rules, that is, the whole test The precompiled variable sale cannot appear in CPP_ ITEM_ H variable with the same name
    • It is common practice to write precompiled variables in uppercase of header file names, which is intuitive and not easy to compare with test CPP conflicts with precompiled variables of other header files

2. Use Header File Protector: ifdef/ifndef

ifdef XXX: if the precompiled variable XXX has been defined, execute the code block between the instruction and endif

ifndef XXX: if the precompiled variable XXX has not been defined, execute the code block between the instruction and endif

Conventional writing

//Sale_Item.h
#ifndef SAL_ITEM_H / / if SAL_ITEM_H is not defined
#define SAL_ITEM_H / / define SAL_ITEM_H precompiled variables as tags (label).
//If ` test CPP ` if the header file has been referenced before, SAL_ITEM_H has been defined
//Under the action of ifndef, the subsequent sales_ Item. H files will not be executed
#include<string>
int i;
struct Sal_Item{
    std::string bookname;
    double renenue;
    int copies;
};
#endif / / terminator

3. Discussion on the necessity of using conditional compilation

  • Perhaps the reader will have a question: since you quote sales repeatedly_ Item. H will cause test CPP error, then do not repeat the reference is not on the line, why bother writing conditional compilation
  • In the above example, Sale_Item.h does not write conditional compilation, but in many cases, we have to repeatedly reference the same header file.
  • Take the above example as an example, for the header file string.
    • In Sale_Item.h, we #include < string > to define a variable std::string bookname
    • In test In CPP, we #include < string > to define a variable std::string name to assign values to objects
    • So test CPP actually refers to the header file string twice, one explicitly and one implicitly when #include "sales_item. H" [sales_item. H also includes string]
  • Therefore, whether it is necessary or not, we recommend that you habitually use conditional compilation when writing header files

Summary: create your own header file

//Sale_Item.h
#ifndef SAL_ITEM_H / / if SAL_ITEM_H is not defined
#define SAL_ITEM_H / / define SAL_ITEM_H precompiled variables as tags (label).
//If ` test CPP ` if the header file has been referenced before, SAL_ITEM_H has been defined
//Under the action of ifndef, the subsequent sales_ Item. H files will not be executed
#include<string>
int i;
struct Sal_Item{
    std::string bookname;
    double renenue;
    int copies;
};
#endif / / terminator
//test.cpp
#include<iostream>
#include<string>
#include"Sale_Item.h"
// #include"Sale_Item.h" / / it doesn't matter because conditional compilation is performed
using namespace std;
int main(){
    Sale_Item data;
    std::string name = "xxx";
    data.bookname = name;
    return 0;
}

Added by vbzoom.com on Tue, 11 Jan 2022 15:57:23 +0200