C + + Coding specification

Source: Internet
Author: User

C + + Coding specification

Coding specification, which says code quality is an important part, how to prevent circular reference? Prevent recompilation every time. Minimal compilation of code modifications.

Not only a simple variable writing format, but more how the function parameters should be specified,
How should the use of pointers be included? Why prohibit the use of default functions, etc...


C + + coding style and common error: C + + is very flexible, it is this flexibility that leads to a lot of errors, as well as the errors caused by non-canonical coding. Standard coding is the basic quality of qualified programmers. We will analyze the error-prone analysis in coding.



1 header file dependent on forward declaration

1 Forward declaration: When we want to use another class object in our header file, we just need to declare class ClassName, no need to use # include "ClassName"

Advantage:

1 Lack of file dependencies, save compilation time

2 dependency relationship of the more explicit classes

Use forward statements as much as possible


#pragma onceclass tea; In the header file class outside the forward declaration class Test{public:test (); ~test (); void sss (tea AA); The function parameter uses this class but not the header file};





2 declaration of function parameters familiarity

A input parameter (passed to the function) before the output parameter (function internal assignment, etc.) after

b input parameters are generally referred to as values and constants

C Output parameters or input/output parameters are typically very few pointers

Facilitates code merging, through the role of positional explicit parameters

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/9D/73/wKioL1mAPW-zzzqMAADKb0fOP7s155.png-wh_500x0-wm_ 3-wmp_4-s_2623660786.png "title="%Y (9x5pku1euaz9eqjc% $VT. png "alt=" wkiol1mapw-zzzqmaadkb0fop7s155.png-wh_50 "/ >





3 Header file contains order

1 First contains the header file corresponding to the class

2 C System Header file

3 C + + system header file

4 Other Library header files

5 header files in this project

The readability of the code is increased by separating between the spaces.


The header file of the class is the CPP in which the class is implemented, so he first contains

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/9D/73/wKiom1mAPj_Rmk53AAHHbr9wIME857.png-wh_500x0-wm_ 3-wmp_4-s_4284990831.png "title=" B ' {{3cx4~12 ((1g_{[) 4ja.png "alt=" wkiom1mapj_rmk53aahhbr9wime857.png-wh_50 "/ >





4 local variable initialization

In the smallest possible role in the general declaration variable, the position of the first use is the better.


Local variables of a class are constructed and refactored by default, and care should be taken to avoid multiple initializations and

Destruction.

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/9D/73/wKiom1mAP1vSaT_RAACSUhvWNOA411.png-wh_500x0-wm_ 3-wmp_4-s_2045761524.png "title=" (a{8f~yt~dph82 ' Nu[z2_lh.png "alt=" wkiom1map1vsat_raacsuhvwnoa411.png-wh_50 "/ >



Class 5 member initialization

Note that initialization of class members, although simple, can cause

The consequence of not estimating, to form a good class member declaration habit



Declaration Order for Class 6

The declarations of functions and members in a class are divided into 3 fast, namely: Public,protected,paivate

Code encapsulation is good, debugging is helpful

The declaration order is as follows:

1 typedef and ENUM

2 Constants

3 constructors

4 destructors

5 member functions, with static member functions

6 data members, with static data members



7 Writing a short function

If there is no special requirement, the function length is controlled at about 40 rows, too long does not affect the program to run the case, split this function >

Extract duplicate code to make it easier for others to read and modify, to find and locate bugs



8 The input reference parameter needs to be on the const

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/9D/73/wKiom1mARU-iVNmuAABkFmC9ds8597.png-wh_500x0-wm_ 3-wmp_4-s_1284587326.png "title=" ~) {xm[zkn) SAYK24MC2E@5C.png "alt=" wkiom1maru-ivnmuaabkfmc9ds8597.png-wh_50 "/ >



9 Self-increment auto-decrement operation

Q






This article is from the "12148490" blog, please be sure to keep this source http://12158490.blog.51cto.com/12148490/1952735

C + + Coding specification

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.