Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What problems should be paid attention to in the .hpp file in C++?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "which problems should be paid attention to in the HPP file in C++". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Hpp, its essence is to mix the implementation code of .cpp into the .h header file, and the definition and implementation are included in the same file, then the caller of this class only needs to include the hpp file, and there is no need to add cpp to project to compile.

The implementation code will be compiled directly into the caller's obj file, instead of generating a separate obj

The adoption of hpp will greatly reduce the number of cpp files and compilations in project, and will no longer have to publish annoying lib and dll, so it is very suitable for writing common open source libraries.

1. It is the abbreviation of Header Plus Plus. 2. Similar to * .h, hpp is the C++ program header file. 3. It is a special header file for VCL and has been pre-compiled. 4. Is the header file of the general template class. 5. Generally speaking, there are only declarations and no implementations in * .h, while all implementations are declared in * .hpp, which can reduce the number of .cpp. 6. * .h can have using namespace std, but not in * .hpp.

* questions to pay attention to in .hpp are:

A) cannot contain global objects and global functions

Because hpp is essentially a .h caller include, when there are global objects or global functions in the hpp file, and the hpp is include by multiple callers, it will result in symbol redefinition errors at link time. To avoid this, you need to remove the global object and encapsulate the global function as a static method of the class.

B) non-circular calls between classes

In the .h and .cpp scenarios, when there is a circular invocation relationship between two or more classes, you only need to declare the called class in the header file in advance.

C) static members cannot be used

The limitation of the use of static members is that if the class contains static members, static member initialization code must be added to the hpp. When the hpp is include by multiple documents, a symbol redefinition error will occur. The only exception is the const static integer member, because in vs2003, this type allows initialization at definition time, such as:

This is the end of the content of "which problems should be paid attention to in the .hpp file in C++". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report