High quality C++/C Programming Guide-11th chapter-Other programming experience (1)

Source: Internet
Author: User
Tags constant modifier

11.1 Using const to improve the robustness of functions
See the Const keyword, the first thing a C + + programmer can think of is a const constant. This is not a good reflex. If you only know to define constants with const, then it is the equivalent of using gunpowder only to make firecrackers. The greater charm of the const is that it can modify the function's parameters, return values, and even the definition of the function. Const is the abbreviation of constant, the meaning of "constant invariant". The things that are modified by the const are protected by coercion, which can prevent accidental changes and improve the robustness of the program. So many C + + programming books suggest: "Use the const whenever you need".

11.1.1 parameters with the const modifier function

If the parameter is output, no matter what data type it is or whether it uses "pointer pass" or "reference pass", it cannot be modified with the const, otherwise the parameter loses its output function.

Const can only modify input parameters:

u if the input parameter is "pointer Pass", then the addition of the Const modifier can prevent accidental changes to the pointer, which is a protective effect.

For example, the Stringcopy function:

void Stringcopy (char *strdestination, const char *strsource);

Where strsource is an input parameter, strdestination is an output parameter. After adding a const modifier to the strsource, the compiler will point out an error if the statement in the body of the function attempts to change the contents of the strsource.

If the input parameter is "value passed", the input parameter is inherently unprotected because the function automatically generates a temporary variable to copy the parameter, so do not add a const modifier.

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

For a parameter that is not an internal data type, a function declared like void Func (a) is destined to be more efficient than the bottom. Because the body of a function will produce a temporary object of type A to copy parameter A, the construction, replication, and destructor of the temporary object will consume time.

For efficiency, you can change the function declaration to void Func (A &a), because reference passing only borrows the alias of the argument and does not need to produce a temporary object. But the function void Func (a &a) has a disadvantage: "Reference passing" is likely to change parameter A, which we do not expect. It is easy to solve this problem by adding a const modifier, so the function eventually becomes void Func (const A &a).

And so on, should void Func (int x) be rewritten as void Func (const int &x) to increase efficiency? Absolutely unnecessary, because the parameters of the internal data type do not have a construction, a destructor, and replication is very fast, and the efficiency of value passing and reference passing is almost equal.

The problem is so lingering that I have to summarize the usage of the "const &" modifier input parameters, as shown in table 11-1-1.

For input parameters that are not internal data types, you should change the way value is passed to const reference delivery to increase efficiency. For example, change void Func (a) to void Func (const a &a).

For input parameters for internal data types, do not change the way value is passed to const reference delivery. Otherwise, it can not achieve the goal of improving efficiency, but also reduce the comprehensible function. For example, void Func (int x) should not be changed to void Func (const int &x).

Table 11-1-1 Rules for "Const &" to decorate input parameters

11.1.2 return value with the const modifier function

If you add a const modifier to the function return value of the "pointer Pass" method, the contents of the function return value (that is, the pointer) cannot be modified, and the return value can only be assigned to the same type of pointer with the const modifier.

such as functions

const char * GetString (void);

The following statement will present a compilation error:

Char *str = GetString ();

The correct usage is

const char *STR = GetString ();

U if the function return value is "value delivery", the const modifier has no value because the function copies the return value to an external temporary storage cell.

For example, do not write the function int GetInt (void) as a const int GetInt (void).

Similarly, do not write function a geta (void) as const a geta (void), where a is a user-defined data type.

If the return value is not an intrinsic data type, rewriting function a geta (void) to const A & Geta (void) does improve efficiency. But you must be careful at this point, be sure to find out whether the function is to return an object "copy" or only return "alias" on it, otherwise the program will be wrong. See section 6.2, "Rules for returning values".

There are not many occasions when the return value of the U function uses "reference pass", which is usually only found in the assignment function of the class, in order to realize the chain expression.

For example

Class A

{...

A & operate = (const a &other); Assignment function

};

A, B, C; A, B, C is the object of a

...

A = B = C; A normal chained-value assignment

(A = b) = C; An abnormal chain assignment, but the legal

If you modify the return value of an assignment function with a const modifier, the contents of the return value are not allowed to be altered. In the example above, statement a = B = c is still correct, but the statement (a = b) = c is illegal.

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.