. hpp File

Source: Internet
Author: User

the meaning of HPP in C + +

In the past in the open source code encountered, today to see the boost source time and met, so learn a bit.

HPP, computer terminology, a header file written in C + + languages, typically used to define data types, declaring variables, functions, structures, and classes. 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.

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 inside can have using namespace std, but *.hpp inside is none.

HPP issues to be aware of

*.HPP issues to be aware of are:

A) cannot contain global objects and global functions

Since HPP is essentially a. h callee include, there are global objects or global functions in the HPP file, and the HPP is used by multiple

When the caller include, the symbolic redefinition error is caused when the link is linked. To avoid this situation, you need to remove the global object, seal the global function

Static methods that are loaded as classes.

b) Non-cyclic invocation between classes

In the. h and. CPP scenarios, when there is a circular call relationship between two classes or multiple classes, the declaration of the called class is made beforehand in the header file

Can be, 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);

}

c) 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)

One, when there is only one static member in the class, and only one caller, you can simulate the local static variables

Method simulation gets a static member

SomeType GetMember ()

{

static SomeType value (XXX);//variable in scope

return value;

}

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 methods to invoke.

SomeType Getmembera ()

{

static SomeType value (XXX);//variable in scope

return value;

}

SomeType Getmemberb ()

{

static SomeType value (XXX);//variable in scope

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 ();

};

The second method is common for most situations, but when the required number of static members is too large, 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;//

}

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;//becomes normal member

}

The interface class provided to the caller

Class a{

Public

Type GetMember ()

{

Return singleton< Aprovider>::getinstance ()->getmember ();

}

}

. hpp File

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.