Farewell to CPP & H: HPP file writing experience)

Source: Internet
Author: User
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 to implement. cpp. Code In the. h 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, no need
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 calls
The number of CPP files and the number of compilations in the project, and the annoying lib and DLL do not need to be released. Therefore, 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 ();};
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 compilation in CPP. Therefore, the inter-class adjustment must be sorted in HPP.
Use relationship, and do not generate loop calls. 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); // static variable return value in the scope;} sometype getmemberb ()

{Static sometype value (XXX); // return value of the static variable in the scope;} void accessmembera ()

{Sometype member = getmembera (); // get static members}; // 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 is huge. In this case, the singleton mode is recommended, define the called class as a normal class, and then use Singleton to change it to a globally unique object for calling. 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 ();}}

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.