How can I prevent header files from being repeatedly referenced?
There are two methods:
1. Method 1:
Use: # pragma once
2> Method 2:
Use: # ifndef # define # endif
Example:
# Ifndef XXXXXXX (a name should be unique as far as possible)
# Define XXXXXXX (same as the above name)
Last file write:
# Endif
3> for # pragma once, according to The MSDN explanation, it can prevent a file from being contained multiple times. Compared with # ifndef # define # endif file protection,
The former (# pragma once) is platform-related and has poor portability, but it is more efficient because it does not need to open the contained file to determine whether the file is contained. Of course, this work is done by the system for us.
The advantage of the latter (# ifndef # define # endif) is that it is a language-related feature, so it is highly portable. However, when a file is contained, you only need to open the file and determine whether the file has been included based on whether the file's protection macro has been defined. Relatively low efficiency. Of course, when # include is used, the programmer can determine whether the protection macro of the file to be included has been defined to determine whether to include the file.
# The ifndef method depends on the macro name and cannot conflict with each other. This not only ensures that the same file is not included multiple times, but also ensures 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 can hardly find the declaration.
# 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. However, repeated inclusion is easier to detect and correct than the "no declaration found" problem caused by macro name collision.
From: http://hi.baidu.com/luckyhare/blog/item/80bafe125b1f8b5cf819b884.html