In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to prevent the repeated introduction of header files in C++ related knowledge, the content is detailed and easy to understand, easy to operate, and has a certain reference value. I believe that after reading this C++ article on how to prevent header files from being repeatedly introduced into the article will be rewarded, let's take a look.
1) use macro definitions to avoid repeated introduction
In actual multi-file development, we often use the following macro definition to avoid repeated introduction:
# ifndef _ NAME_H#define _ NAME_H// header file content # endif
Where _ NAME_H is the name of the macro. It is important to note that the macro names set here must be unique and not the same as those of other macros in the project.
When the file is first # include in the program, since _ NAME_H is not defined, the code in the "header contents" section is defined and executed; when # include occurs more than once, the code in the "header contents" section is not repeated because the _ NAME_H has already been defined.
That is, we can modify the student.h file in the previous project as follows:
# ifndef _ STUDENT_H#define _ STUDENT_Hclass Student {/ /.}; # endif
Although the project still # include "student.h" twice in the main.cpp file, the _ STUDENT_H macro can only be defined once, so the Student class can be defined only once. If you execute the project again, you will find that it can be executed normally.
2) use # pragma once to avoid repeated introduction
In addition to the first most commonly used method above, you can use the # pragma one directive to append it to the beginning of the specified file, and the file will only be # include once.
We know that # ifndef avoids repetition by defining unique macros, which means that headers are identified every time they are introduced, so it is inefficient. However, considering that both C and C++ support macro definitions, the use of # ifndef in the project to avoid the possible problem of "repeated introduction of header files" will not affect the portability of the project.
Compared with ifndef, # pragma once does not involve macro definition, and when the compiler encounters it, it immediately knows that the current file is only introduced once, so it is very efficient.
However, it is worth mentioning that not every version of the compiler can recognize the # pragma once directive, and some older compilers do not support this directive (warning will be issued during execution, but compilation will continue), that is, the # pragma once instruction is not very compatible.
At present, almost all common compilers support the # pragma once directive, even when Visual Studio 2017 creates a new header file. It can be said that # pragma once is a non-standard instruction that is gradually supported by many compilers in CCompact +.
In addition, # pragma once can only work on a specific file, not on a specified piece of code like # ifndef.
Here again, take the previous "student.h" file as an example, and modify its contents to:
# pragma onceclass Student {/ /.}
If you run the project again, it can also be executed normally.
3) use the _ Pragma operator
The C99 standard adds a new _ Pragma operator similar to the # pragma directive, which can be seen as an enhanced version of # pragma, which not only implements all the functions of # pragma, but more importantly, _ Pragma can also be used with macros.
This section does not go into detail about the more functions and usage of the _ Pragma operator, but only how to use the _ Pragma operator to avoid repeated introduction of header files.
When dealing with the problem of repeated introduction of header files, you can add the following statement to the beginning of the corresponding file:
_ Pragma ("once")
For example, add this statement to the beginning of the student.h file in the previous project, execute the project again, and it will execute normally.
In fact, in order to prevent users from introducing system library files repeatedly, almost all library files adopt one of the above three structures, which is why the compiler of system library files will not report an error.
This is the end of the article on "how to prevent header files from being introduced repeatedly in C++". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to prevent header files from being introduced repeatedly in C++". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.