. HPP file in C ++

Source: Internet
Author: User

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 CPP 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.

1. It is short for header plus.

2. Like *. H, HPP is the c ++ program header file.

3. It is a VCL-specific header file that has been pre-compiled.

4. It is the header file of the general template class.

5. In general, only declarations in *. h are not implemented, while declarations in *. HPP are implemented. The latter can reduce the number of. cpp.

6. Using namespace STD can be found in *. h, but not in *. HPP.

7. * HPP should pay attention to the following issues:

A) Global Objects and global functions cannot be included.

Because HPP is essentially used as the. h caller to include, when the HPP file contains a global object or a global function, the HPP is

When the caller includes, the symbolic redefinition error will occur during the link. To avoid this situation, you need to remove global objects and seal global functions.

Static Method installed as a class.

B) non-circular calls between classes

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

As follows:

Class B;

Class {

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, instead of waiting for the CPP

. Therefore, the call relationship between classes must be organized in HPP, and circular calls cannot be generated. Similarly, when two classes A and B are defined in their respective

In the HPP file, loop calls such as the following will also cause compilation errors:

// A. HPP

# Include "B. HPP"

Class {

Public:

Void somemethod (B );

};

// B. HPP

# Include "A. HPP"

Class B {

Public:

Void somemethod ();

}

C. Do not use static members.

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, because in vs2003, this type can be initialized during definition, for example:

Class {

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 methods in the same class)

1. When there is only one static member in the class and there is only one caller, the local static variables can be used to simulate

// Obtain static members using simulated Methods

Sometype getmember ()

{

Static sometype value (XXX); // static variable in the scope

Return value;

}

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 in the scope

Return value;

}

Sometype getmemberb ()

{

Static sometype value (XXX); // static variable in the scope

Return value;

}

Void accessmembera ()

{

Sometype member = getmembera (); // get static members

};

// Obtain two static members

Void accessstaticmember ()

{

Sometype A = getmembera (); // get static members

Sometype B = getmemberb ();

};

III. The second method is common in most cases. However, when too many static members are required, the workload for writing encapsulation methods will be very high.

Huge. In this case, we recommend that you use the singleton mode to define the called class as a normal class, and then use Singleton to change it

A globally unique object is called.

For example, the definition in the original H + CPP is as follows:

Class {

Public:

Type getmember (){

Return member;

}

Static type member; // static member

}

Using Singleton, the implementation code may be as follows (for Singleton implementation, please refer to the relevant documentation)

// Actual implementation class

Class aprovider {

Public:

Type getmember (){

Return member;

}

Type member; // It is a common member.

}

// Interface class provided to the caller

Class {

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.