Friday, October 8, 2010

High-quality C + + / C Programming Guide - Chapter 11 references the experience of other programming

Chapter 11 Other programming experience

11.1 Use const to improve the robustness of the function

See the const keyword, C + + programmers first thought may be constant const. This is not a good reflex. If you only know the definition of constants with const, then the equivalent of the powder is only used to make firecrackers. const more attractive is that it can modify the function parameters, return values, and even the definition of the function body.

const is a constant of the abbreviation, "constant" means. Const everything was modified by the force protection, to prevent accidental changes to improve the process robustness. Therefore, many C + + programming book proposal: "Use const whenever you need".

11.1.1 Modified function with const parameters

If the parameter for the output to use, no matter what it is data type, and whether it uses "pointer passed" or "passing reference" can not add const modified, otherwise the argument will lose output.

const can only modify the input parameters:

u If the input parameter a "pointer passed", then add const modified to prevent accidental changes to the pointer, has protective effects.

For example StringCopy function:

void StringCopy (char * strDestination, const char * strSource);

One strSource are input parameters, strDestination is output parameter. Add const to strSource modified, if the statement is trying to change the function body strSource content, the compiler will point out the error.

u If the input parameter used "value transfer", because the function will automatically generate temporary variables for the replication of the parameters, the input parameters would have no need to protect, so do not add const modified.

For example, do not function void Func1 (int x) written as void Func1 (const int x). Similarly do not function void Func2 (A a) written void Func2 (const A a). Where A is a user-defined data types.

u for non-internal data type parameters, such as void Func (A a) the efficiency of such comparative statement of the function bound to the end. Because the function body will have a temporary object of type A for the replication parameters a, and the construction of temporary objects, copy, destructor will be time-consuming process.

To improve efficiency, you can change the function declaration void Func (A & a), because "passing reference" to borrow only what alias parameters only, do not need to create temporary objects. But the function void Func (A & a) there is a drawback: "reference transfer" could change the parameters a, which we do not expect. To solve this problem is easy, you can add const modification, so the function eventually become void Func (const A & a).

And so, it should be void Func (int x) rewritten as void Func (const int & x), in order to improve efficiency? Not necessary, because the internal data structure of type parameter does not exist, the process of destruction, and reproduction is also very fast, "value transfer" and "passing reference" the efficiency of the almost equal.

The problem is so touching, I had to be "const &" the use of modified input parameters to sum up, as shown in Table 11-1-1.

For non-internal data type of input parameters, should be "value transfer" approach to "const reference to pass," aims to improve efficiency. Such as void Func (A a) to void Func (const A & a).

Internal data type for input parameters, not the "value transfer" approach to "const reference to pass." Otherwise, not only fail to improve efficiency, but also reduce the function of intelligibility. For example void Func (int x) should not be changed to void Func (const int & x).

Table 11-1-1 "const &" Modified input parameters of the rules

11.1.2 use the const return value modified

if u give "pointer passed" means plus function return value const modification, then the function returns the value (ie, pointer) of the content can not be modified, the return value can only be assigned to additional modification of the same type const pointer.

For example, the function

const char backup bin conf config data eshow_sitemap.html log maint sitemap.html svn tmp GetString (void);

Statement will be a compilation error as follows:

char * str = GetString ();

Correct usage is

const char * str = GetString ();

u if the function returns a "value transfer mode", since function will return value copied to an external temporary storage unit, no value added const modified.

For example, not to function int GetInt (void) write const int GetInt (void).

Similarly not to function A GetA (void) write const A GetA (void), in which A is a user-defined data types.

If the return value is not within the data type, the function A GetA (void) rewritten as const A & GetA (void) does improve efficiency. But never, never to be careful at this time, we must find out whether the function returns an object to the "copy" or just return to "Alias" on it, otherwise the process will go wrong. See section 6.2 "Rules of the return value."

u function returns the value a "passing reference" not many occasions, this approach generally appear in the class assignment function, the objective is to achieve chain expression.

Such as

class A


A & operate = (const A & other); / / assign function


A a, b, c; / / a, b, c for the A's object


a = b = c; / / normal chain assignment

(A = b) = c; / / not normal chain assignment, but the legal

If you assign the return value added const modification, then the return value is not allowed to change the content. The above example, the statement a = b = c is still correct, but statement (a = b) = c is illegal.

11.1.3 const member function

Does not modify any data member of the function should be declared as const type. If in the preparation of const member function, accidentally modify data members, or call the other non-const member functions, the compiler will indicate an error, it will undoubtedly improve the process robustness.

The following program, class member function GetCount stack used only for counting, logically, GetCount function should be const. Compiler will point out the error GetCount function.

class Stack



void Push (int elem);

int Pop (void);

int GetCount (void) const; / / const member functions


int m_num;

int m_data [100];


int Stack:: GetCount (void) const

+ + M_num; / / Compile error, attempting to modify the data members of the m_num

Pop (); / / Compile error, attempting to call non-const function

return m_num;


const member function declaration looks strange: const keyword on the function declaration can only be the tail, presumably because other places have been occupied.

11.2 efficiency of the process
Timing of the process efficiency is the speed, efficiency refers to the process of space occupied by the status of memory or external memory.

Overall efficiency is the standing point of the entire system to consider the efficiency of the local efficiency is the point of standing on the module or function to consider efficiency.

11-2-1銆?銆恟ules do not blindly pursue the efficiency of procedures, should meet the accuracy, reliability, robustness, readability, quality factors such as the premise, to efficiency of the process.

11-2-2銆?銆恟ules to improve the overall efficiency of the program mainly to improve the efficiency of the local secondary.

Rule 11-2-3銆?銆恊fficiency in the optimization process, you should first find out the restrictions on the efficiency of the "bottleneck", do not does not matter between optimization.

Rule 11-2-4銆?銆恌irst optimized data structures and algorithms, and then optimize the code.

Rule 11-2-5銆?銆恡ime efficiency and space efficiency is sometimes possible confrontation that should be analyzed at this time is more important to make appropriate trade-off. For example, take some more memory to improve performance.

11-2-6銆?銆恟ules do not pursue a compact code because the code does not produce compact and efficient machine code.

11.3 Some useful suggestions
Beware of those who proposed 11-3-1銆?銆恦isually difficult to distinguish the operator to write error occurred.

We often will "==" write error "=" symbols such as "","&&","<=",">=" also very prone to "throwing a" mistake. However, the compiler does not necessarily automatic that such an error.

11-3-2銆?銆恜roposed variables (pointers, arrays) have been created, initialize them should be promptly, to prevent the uninitialized variable as the right value to use.

Beware of the proposed 11-3-3銆?銆恑nitial value of variables, the default value of the error, or precision is not enough.

Beware of the proposed 11-3-4銆?銆怐ata type conversion error. To make use of explicit data type conversion (to let people know what happened), to prevent the compiler light quietly implicit data type conversions.

Beware of the proposed 11-3-5銆?銆恦ariable overflow or underflow occurs, the array subscript bounds.

Beware of the proposed 11-3-6銆?銆恌orget write error handler, be careful error handling process itself is wrong.

Beware of the proposed 11-3-7銆?銆恌ile I / O errors.

Proposed 11-3-8銆?銆恆void writing skills of a high code.

11-3-9銆?銆恟ecommended not to design every aspect, very flexible data structure.

11-3-10銆?銆恜roposed code quality if the original is better, try to reuse it. However, very bad, do not fix the code, should be re-written.

11-3-11銆?銆恜roposed to make use of standard library functions, not "invention" already exists in the library functions.

11-3-12銆?銆恟ecommended not to use specific hardware or software environment with the closely related variables.

11-3-13銆?銆恟ecommended compiler options to set the most stringent state item.

11-3-14銆?銆恟ecommended, if possible, use the PC-Lint, LogiScope tools such as code review.

[Cline] Marshall P. Cline and Greg A. Lomow, C + + FAQs, Addison-Wesley, 1995

[Eckel] Bruce Eckel, Thinking in C + + (C + + programming ideas, Zong-Tian Liu, M.), Machinery Industry Press, 2000

[Maguire] Steve Maguire, Writing Clean Code (programming essence, Jiang Jingbo, M.), Electronics Industry Press, 1993

[Meyers] Scott Meyers, Effective C + +, Addison-Wesley, 1992

[Murry] Robert B. Murry, C + + Strategies and Tactics, Addison-Wesley, 1993

[Summit] Steve Summit, C Programming FAQs, Addison-Wesley, 1996


Negative Comments: the right decision, "patron saint"

Shenzhen, Hong Kong: forerunner of attitude

Wizard Multimedia CREATION Tools

BOE Holdings East TPV regulators have not yet ratified Difficult

3G2 to AVI

find the best registry cleaner software for your

EASY Graphic

reviews Games Sports

Common operation OF FreeBSD 5.2

3ds Max Cattle Production Throughout Korean Beauty Song Hye Kyo

To. NET migration risk Assessment [1]

3G2 to WMV


Audio Format CHANGES for

Good Animation Tools

Ma: Alibaba revolutionized the traditional

Six college students recruited reef workplace

1 comment: