HPP, the essence of which is to mix the. CPP implementation code in the. h header file, where the definition and implementation are contained in the same file, the caller of the class needs to include the HPP file only, eliminating the need to add CPP to project for compilation. The implementation code is compiled directly into the caller's obj file, and no separate obj is generated, and HPP will drastically reduce the number of CPP files and compile times in project, as well as the annoying Lib and DLL releases, making it ideal for writing common open source libraries. HPP has a lot of advantages, but the following points to note in writing:
1, is the header plus plus the shorthand.
2, similar to *.h, HPP is a C + + program header file.
3, is the VCL Dedicated header file, has been pre-compiled.
4, is the general template class header file.
5, in general, *.h inside only declarations, not implemented, and *.HPP declaration of implementation are, the latter can reduce the number of. cpp.
6, *. H can have a using namespace std, while the *.HPP is not.
7, cannot contain global objects and global functions. since HPP is essentially a. h callee, there are global objects or global functions in the HPP file that, when used by multiple callers, will cause symbolic redefinition errors when linked. To avoid this situation, you need to remove the global object and encapsulate the global function as a static method of the class.
8. There is no cyclic call between classes. In the . H and. CPP scenarios, when there is a circular call relationship between two classes or classes, simply make a declaration of the called class beforehand in the header file, as follows: class B; class a{ Public : void SomeMethod (b b); }; class b{ Public : void SomeMethod (a a); }; in the HPP scenario, since both the definition and the implementation already exist in a file, the caller must know all the definitions of the callee explicitly and not wait until the CPP is compiled. So HPP must tidy up the call relationship between classes and not produce circular calls. Similarly, for two classes A and B are defined in their respective HPP files, circular calls that are shaped as follows will also result in compilation errors: //a.hpp #include "b.hpp" class a{ Public : void SomeMethod (b b); }; //b.hpp #include "a.hpp" class b{ Public : void SomeMethod (a a); };
9. Do not use static members. the limitation of the use of static members is that if a class contains static members, it is necessary to include static member initialization code in HPP, which produces symbolic redefinition errors when the HPP is included in multiple documents. The only exception is the const static integer member, because in vs2003, the type allows initialization at definition, such as:class a{Public :const static int intvalue = 123; }; because the use of static members is a common scenario and cannot be forced to purge, there are several ways to consider this (the following examples are methods in the same class)1. When there is only one static member in the class and only one caller, you can simulate the local static variables //method simulation get static member someType getmember () { static sometypevalue (XXX); In-scope static variables return value; } 2. When there are multiple methods in a class that require a call to a static member, and there may be multiple static members, each static member can be encapsulated by an impersonation method for other method invocations. someType Getmembera () { static sometypevalue (XXX); In-scope static variables return value; } someType getmemberb () { static sometypevalue (XXX); In-scope static variables return value; } void Accessmembera () { SomeType member = Getmembera (); Get static members }; //Get two static members void Accessstaticmember () { SomeType a = Getmembera (); Get static members someType B = Getmemberb (); }; 3. The second method is common for most situations, but when there are too many static members required, the effort to write the encapsulation method is enormous, in which case it is recommended to use the singleton pattern to define the called class as a normal class, The call is then made using singleton to make it a globally unique object. as the original h+cpp is defined as follows: class a{ Public : Type GetMember () { return member; } static type member; Static members } using the Singleton method, the implementation code may be as follows (Singleton implementation please consult the relevant documentation yourself)//Actual implementation class class aprovider{ Public : Type GetMember () { return member; } type member; //Become a regular member } //interface class provided to the caller class a{ Public : Type GetMember () { return singleton<aprovider >::getinstance ()->getmember (); } }
The difference between the HPP header file and the H header file