. hpp files and. h files in C + +

Source: Internet
Author: User

In view of the ASN-GW project file source code, found the *.hpp file, very strange, before in the textbooks have not studied, in Baidu, and found a point:

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, generally speaking, *.h inside only the declaration, did not implement, and *.HPP declaration implementation has, the latter can reduce the number of. cpp.

6, *.h inside can have using namespace std, and *.hpp is not.

7, *.HPP to pay attention to the problems are:

A) cannot contain global objects and global functions

Because HPP is essentially an. h callee include, there is a global object or global function in the HPP file, and the HPP is more than

When caller include, the symbol redefinition error is caused when the link occurs. To avoid this situation, you need to remove the global object and block the global function

A static method that is loaded as a class.

b) can not be called between classes

In the scenario of. H and. cpp, when there is a circular call between two classes or classes, as long as the declaration of the invoked class is done in advance in the header file

Can be, as follows:

Class B;

Class a{

Public

void SomeMethod (b b);

};

Class b{

Public

void SomeMethod (a);

};

In the HPP scenario, because the definition and implementation already exist in a file, the caller must be aware of all the definitions of the callee and not wait until the CPP

To compile the. Therefore, you must organize calls between classes in HPP, and you cannot generate circular calls. Similarly, for when two classes A and B are defined separately in the respective

HPP file, circular calls that are shaped like the following 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);

}

c) cannot use static members

The limit to the use of static members is that if the class contains static members, static member initialization code must be added to the hpp, and a symbolic redefinition error is generated when the HPP is included with multiple documents. The only exception is a const static integral 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 example is the same class method)

When there is only one static member in a class, and only one caller, it can be simulated by a local static variable

Method simulates getting static members

SomeType GetMember ()

{

static SomeType value (XXX);//Scope statics

return value;

}

Ii.. When there are multiple methods in the class that require static members to be invoked, and when multiple static members may exist, each static member can be encapsulated into a mock method for invocation by other methods.

SomeType Getmembera ()

{

static SomeType value (XXX);//Scope statics

return value;

}

SomeType Getmemberb ()

{

static SomeType value (XXX);//Scope statics

return value;

}

void Accessmembera ()

{

SomeType member = Getmembera ();//Get static members

};

Get two static members

void Accessstaticmember ()

{

SomeType a = Getmembera ();//Get static member

SomeType B = Getmemberb ();

};

The second method is common to most cases, but when there are too many static members required, the amount of work to write the encapsulation method will be very

Great, in this case, it is recommended to use the singleton mode, define the called class as a normal class, and then use Singleton to change it to

The globally unique object is invoked.

The definition of the original h+cpp is as follows:

Class a{

Public

Type GetMember () {

return member;

}

Static type member;//statically member

}

Using the Singleton method, the implementation code may be as follows (singleton the implementation please check the relevant documents)

Actual implementation class

Class aprovider{

Public

Type GetMember () {

return member;

}

Type member;//into an ordinary member

}

Interface class provided to the caller

Class a{

Public

Type GetMember () {

Return Singleton<aprovider>::getinstance ()->getmember ();

}

}

Original address: http://ieoqmdce.blog.163.com/blog/static/38939954200961510477930/

Related Article

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.