Some programming specifications of C ++ (based on google)

Source: Internet
Author: User

1. Use # define to prevent multiple inclusion of all header files. For the naming format, see <PROJECT >_< PATH >_< FILE> _ H.

2. Use the pre-declaration to minimize the number of # include in. H files, resulting in unnecessary re-compilation.

 

3. function parameters: the input parameter is in the front and the output parameter is in the back. Input parameters are generally transmitted as values or constant references. output parameters or input and output parameters are non-constant pointers.

4. cpp header file inclusion Sequence

Header file with the same name

C System File

C ++ System File

Other library header files

Other header files in this project

5. namespace

(1) namespace with name and no name

(2) You cannot declare a sub-namespace in The namespace definition. You cannot add a new member to the sub-namespace in the form of "namespace: Sub-namespace {}" outside the namespace.

A. namespace

{

Namespace B; // error

}

B. namespaceA {}

A: B {}; // error

NamespaceA {

Namespace B {}// right

}

(3) using command and using Declaration

Using command: using namespace A; // all the members in the namespace can directly use

Using Declaration: using A: member name // only declare A specific Member

(4) In general, using the using statement is safer. Because the using declaration only imports the specified name. If the name conflicts with a local name, the compiler reports an error. The using command imports the names of all the Members in the namespace, including those that may not be used at all. If a name conflicts with a local name, the compiler does not issue any warning information, but uses a local name to automatically overwrite members with the same name in the namespace. In particular, the openness of namespaces makes it difficult for programmers to know exactly which names are added to a namespace because the members of a namespace may be scattered in multiple places.

(5) aliases can be defined for namespaces.

Namespace alias = namespace name; // convenient to use

6. Local Variables

We recommend that you declare a variable in the smallest possible scope. The closer the variable is to be used for the first time, the better. initialize the variable when declaring it.

7. Declaration Order of Class Members

(1) public, protected, private

(2) typedefs and enums

Constant

Constructor

Destructor

Member Functions

Data Member

8. Use C ++ type conversion instead of C type conversion

(1) static_cast: similar to C-style conversion, it can be forced conversion of values, or a clear upward conversion from the subclass of the pointer to the parent class.

(2) const_cast: remove the const attribute

(3) reinterpret_cast: unsafe conversions between pointer types and integers, or between other pointers, used only when you understand what you have done.

(4) dynamic_cast: runtime type check, pointer parent class to subclass Conversion

 

9. Use pre-auto-increment instead of post-auto-increment. The post-auto-increment must be copied once more.

10. Use as many const as possible

 

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.