HPP, which is equal. H plus. CPP is frequently used in open-source libraries such as boost and xerces. By coincidence, I learned a half-way method and recorded it as follows for reference.
The essence of HPP is. CPP implementation code. h. In the header file, the definition and implementation are included in the same file. Then, the caller of this class only needs to include the HPP file, and no need to add CPP to the Project for compilation. The implementation code will be directly compiled into the caller's OBJ file, and no separate OBJ will be generated. Using HPP will greatly reduce the number of CPP files and the number of compilations in the called Project, it does not need to release annoying lib and DLL, so it is very suitable for compiling public open-source libraries. HPP has many advantages, but note the following points in writing:
1. Global Objects and global functions cannot be included.Because HPP is essentially used. H is called by include. Therefore, when a global object or global function exists in the HPP file and the HPP is included by Multiple callers, the symbolic redefinition error will occur during the link. To avoid this situation, you need to remove global objects and encapsulate global functions as static methods of classes.
2. classes cannot be called cyclically.In. H and. in CPP scenarios, when two or more classes have a circular call relationship, you only need to declare the called class in advance in the header file, as shown below: Class B; class A {public: void somemethod (B) ;}; Class B {public: void somemethod (A) ;}; In the HPP scenario, since the definition and implementation already exist in a file, the caller must clearly understand all the definitions of the caller, rather than waiting for the CPP to compile. Therefore, the call relationships between classes must be organized in HPP, and no cyclic calls can be generated. Similarly, when two classes A and B are defined in their respective HPP files, loop calls such as the following will also cause compilation errors: //. HPP # include "B. HPP "Class A {public: void somemethod (B) ;}; // B. HPP # include ". HPP "Class B {public: void somemethod ();};
3. Static members are not allowed.Static member usage restriction: if the class contains static members, you must add static member initialization code to HPP. When the HPP is included in multiple documents, a symbolic redefinition error is generated. The only exception is the const static integer Member, because in vs2003, this type can be initialized during definition, such as: Class A {public: const static int intvalue = 123 ;}; because static members are used in common scenarios and cannot be forcibly cleared, you can consider the following methods (the following examples are the methods in the same class) 1. when there is only one static member in the class and only one caller can use the local static variable simulation // method to obtain the static member sometype getmember () {static sometype value (XXX ); // return value of the static variable in the scope;} 2. when there are multiple methods in the class that need to call static members, and there may be multiple static members, you can encapsulate a simulation method for each static member for other methods to call. Sometype getmembera () {static sometype value (XXX); // return value of the static variable in the scope;} sometype getmemberb () {static sometype value (XXX ); // static variable return value in the scope;} void accessmembera () {sometype member = getmembera (); // get static member}; // get two static members void accessstaticmember () {sometype A = getmembera (); // obtain the static member sometype B = getmemberb () ;}; 3. the second method is common in most cases. However, when too many static members are required, the workload for writing encapsulation methods will be huge. In this case, we recommend that you use the singleton mode. The call class is defined as a normal class, and then it is called using Singleton to change it to a globally unique object. For example, the definition in the original H + CPP is as follows: Class A {public: Type getmember () {return member;} static type member; // static member} adopts the singleton method, the implementation code may be as follows (for Singleton implementation, please refer to the relevant documentation) // The actual implementation class aprovider {public: Type getmember () {return member;} type member; // become a common member} // Class A {public: Type getmember () {return Singleton <aprovider >:: getinstance () -> getmember ();}}
Differences between. h and. HPP