This article reprinted http://blog.csdn.net/liuzhanchen1987/article/details/7270005, thanks in this
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 precompiled.
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. h and. CPP scenarios, when there are circular call relationships between two classes or classes, just pre-declare the called class 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, because 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 compiles. So HPP must tidy up the call relationship between classes and not produce circular calls. Similarly, for when two classes A and B are defined separately in their respective HPP files, circular calls that are shaped as follows will also cause 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, you cannot use static members. The use of static members is limited to the fact that if a class contains static members, it is necessary to include static member initialization code in HPP, which generates 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 (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 sometype value (XXX);//scope static variable 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 a simulation method For other methods to invoke. sometype getmembera () { Static sometype value (XXX);//scope static variable return value; } sometype getmemberb () { Static sometype value (XXX);//scope static variable 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 generic for most cases, However, when the required number of static members is too large, the effort to write the encapsulation method will be enormous, in which case it is recommended to use the singleton mode, define the called class as a normal class, and then use Singleton to make the call to a globally unique object. as defined under the original h+cpp as follows: class a{ public : tyPe getmember () { return member; } static type member;//Static members } using singleton mode, the implementation code may be as follows (Singleton implementation please consult the relevant documents) // Actual implementation Class class Aprovider{ public : type getmember () { Return member; } type member;//becomes the normal member } //interface class provided to the caller class A{ public : type GetMember () { Return singleton<aprovider >::getinstance ()->getMember (); } }
. HPP and. H Differences