Header file protection

Source: Internet
Author: User

I. # pragma once (commonly used) is a commonly used command, as long as this command is added at the beginning of the header file, the header file can be compiled once. # pragma once is used to prevent a header file from being included multiple times. # ifndef, # define, # endif is used to prevent a macro from being defined multiple times. # Pragma once is related to compilation, that is, it can be used in this compilation system, but not necessarily in other compilation systems, that is, it has poor portability, but now, basically every compiler has this definition. # Ifndef, # define, # endif, which is related to the C ++ language. This is a macro definition in the C ++ language. Using the macro definition, you can avoid multiple compilation of files. Therefore, it is effective in all compilers that support the C ++ language. If the program to be written is cross-platform, it is best to use this method 2. # ifndef ~ # Define ~ # Endif 1: # ifndef SALESITEM_H2: # define SALESITEM_H3: // Definition of Sales_itemclass and related functions goes here4: # The endif header file should contain a protection character, even if these header files are not included by other header files. Writing header file protection characters is not difficult. If the header file is contained multiple times, it can avoid compilation errors that are hard to understand. Before writing header files, we need to introduce some additional pre-processor facilities. The Preprocessor allows us to customize variables. The Preprocessor variable name must be unique in the program. Any use of names that match the Preprocessor variable is associated with the Preprocessor variable. To avoid name conflicts, Preprocessor variables are often expressed in uppercase letters. Preprocessor variables have two states: defined or undefined. The pre-processor variables defined are different from the pre-processor indicators used to detect their statuses. # Define indicates accepting a name and defining it as a Preprocessor variable. # Ifndef indicates whether to check whether the specified Preprocessor variable is undefined. If the Preprocessor variable is undefined, all the instructions following it are processed Until # endif appears. You can use these facilities to prevent multiple times containing the same header file: # ifndef SALESITEM_H # define SALESITEM_H // Definition of Sales_itemclass and related functions goes here # endif condition indication # ifndef itemsales_h test whether the SALESITEM_H Preprocessor variable is undefined. If SALESITEM_H is not defined, # ifndef test is successful. All the rows following # ifndef are executed until # endif is found. Conversely, if SALESITEM_H is defined, # ifndef indicates that the test is false, and the code between the instruction and # endif indicates is ignored. To ensure that the header file is only processed once in the given source file, we first detect # ifndef. When the header file is processed for the first time, the test succeeds because SALESITEM_H is not defined yet. The next statement defines SALESITEM_H. In that case, if the compiled file exactly contains the header file again. # Ifndef indicates that SALESITEM_H has been defined and the rest of the header file is ignored. Header files should contain protection characters, even if these header files are not included by other header files. Writing header file protection characters is not difficult. If the header file is contained multiple times, it can avoid compilation errors that are hard to understand. This policy is effective when no header file is defined and a Preprocessor constant with the same name is used. We can name Preprocessor variables for entities (such as classes) defined in header files to avoid the problem of duplicate Preprocessor variables. A program can contain only one class named Sales_item. Class names are used to form the names of header files and Preprocessor variables, it is possible that only one file will use the Preprocessor variable. 3. Comparison # The difference between pragma once and # ifndef. To avoid the same file being included multiple times, 1 # ifndef Mode 2 # the pragma once method is applicable to compilers that support these two methods, there is no big difference between the two, but there are still some minor differences between the two. Method 1: # ifndef _ SOMEFILE_H __# define _ SOMEFILE_H __...... // some declaration statements # endif Method 2: # pragma once ...... // some declaration statements # The ifndef method depends on the macro name and cannot conflict with each other. This not only ensures that the same file will not be contained multiple times, it can also ensure that two files with identical content are not accidentally included at the same time. Of course, the disadvantage is that if the macro names of different header files are accidentally "crashed", the header files may obviously exist, but the compiler cannot find the declared state # pragma once is guaranteed by the compiler: the same file will not be contained multiple times. Note that the "same file" here refers to a physical file, not two files with the same content. The advantage is that you don't have to think about a macro name any more. Of course, there won't be any strange problems caused by the macro name collision. The disadvantage is that if a header file has multiple copies, this method cannot ensure that it is not repeatedly included. Of course, repeat inclusion is easier to detect and correct than the "no declaration found" problem caused by macro name collision. The first method is supported by the language, so the portability is good. The second method can avoid name conflicts.

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.