C ++ error-prone basic problems

1, overly positive comments

Comments must maintain and code together, things should not be obvious from the description, or to somewhere else already clear things once again.

2, magic number (literal)

No literal semantics, there is no real memory address.

Therefore, whichever not address, nor it can be used to initialize the common reference, for example: long & r1 = 40000;

But const long & r1 = 40000 is legal.

So, try not to use literal, but should use enumeration constants and initialized constant variables

3, global variables

Global variables increases the coupling between modules.

When global variables (static variables) is used to initialize the value can not be calculated properly on at compile time, the action will drag runtime initialization, which may lead to a fatal error. Price patterns can be used to solve this problem

4, and fails to distinguish between the form of function overloading Parameter Default Value

Function overloading is mainly used for the same set of abstract sense, but to achieve different functions.

Default parameters and the main form for simplicity, provide a more concise interface functions.

5, misunderstanding of reference

(1) Since there is no reference to an address, the statement cited reference, pointer to a reference or reference array is illegal.

(2) reference must not have constant or volatile, so can not use the keyword const or volatile modified reference.

(3) Any expression can be as complex values ​​can be left as an initialization object references

(4) char * cp = reinterpret_cast (a); // get the address of the cp error

reinterpret_cast (a) = cp; // correct,

(5) retains a reference to an array of array size information, and a pointer is not (this property is sometimes used in an array as a function name to call the function argument) Reserved

(6) can function declaration references:

ex: int f( double );

int (* const pf) (double) = f; // pf is a pointer to function f () is a constant pointer

int (& rf) (double) = f; // rf is a function f () REFERENCE

The function itself can be a reference or conversion (implicitly) to a pointer to a function, and then use the dereference syntax.

6, constant misunderstanding of (sexual) of

(1) letter did not address the constant, never be left for value

(2) ci int; int const IP2 & ci = const describe only the operation limit by the IP2 ci, ci and not limit the general operation. That ci = 5 can, while ip2 = 5 can not.

7, the subtleties of the C language

(1) If a? Two options left are the result of an expression value, the expression itself is left value.

(2) case statement labels must be integer constant sexual expression (median case will be calculated at compile time)

8, the distinction between accessibility and visibility

(1) placed to provide a variety of suitable pre-declared private header files

(2) the class interface from its implementation separation, so as to achieve true data hidden realm, and it is the only way to bridge the use of design patterns

(3) using a bridge, any modification to achieve Class C, Class C without altering the interface, the influence will be firmly clamped in a separate implementation file.

Published 261 original articles · won praise 4 · Views 4269

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105206029