High quality c ++ programming guide Study Notes chapter 11th-thanks to Lin Rui

Source: Internet
Author: User

Chapter 2 other programming experience

11. 1 use const to improve the robustness of functions

The greater charm of const is that it can modify the parameters, return values, and even the definition bodies of functions.

11.1.1 use const to modify function parameters

If a parameter is used for output, both pointer transmission and reference transmission cannot be modified with Const. Otherwise, the parameter will lose the output function. Const can only modify the input parameters:

Ø
If the input parameter uses "pointer passing", adding const can prevent unexpected changes to the pointer. Pointer passing is essentially a value passing. If a pointer constant is used and a copy of the parameter is generated during the call, its const will be invalid. Therefore, the input parameter is a constant pointer rather than a pointer constant. Const is a constant pointer before *, and then a pointer constant.

Ø
If the input parameter uses "value transfer", because the function will automatically generate a temporary variable for copying this parameter, the input parameter does not need to be protected, so do not add const modification.

Ø
For parameters of non-Internal data types, declarations such as void fun (a) are destined to be less efficient. Because a temporary object of type A is generated in the function body for copying parameter A, it takes time to construct, copy, and analyze the temporary object. To improve efficiency, you can change the Declaration to void.
Fun (A & A) does not need to generate a temporary object because of reference transfer. Disadvantage: The parameter A may be changed for reference transfer. Therefore, you need to change it to void fun (const A & ). For internal data types, because there is no constructor process, you do not need to change it to reference for transmission. In short, for non-Internal data types, the "value transfer" method should be changed to the "const reference transfer" method. For internal types, use "pass value ".

11.1.2 use const to modify the return value of the Function

The above describes three transmission modes of input parameters, and three return methods of functions, which are the same as those of input parameters.

Ø
If the const modifier is added to the return value of a function in the "pointer passing" mode, the content of the function return value cannot be modified. The returned value can only be assigned to the same type of Pointer Modified by the const method. Example: const char * getstring (void );

Char * STR = getstring (); ---------------------- compilation Error

Const char * STR = getstring (); ---------------- correct

You cannot assign a pointer to a constant to a pointer not to a constant.

Ø
If the return value is in the "value transfer" mode, the function copies the return value to an external temporary storage unit and adds the const modifier. For non-Internal data types, you can use "const reference transfer". Note that the reference cannot point to the local variable of the function; otherwise, an error occurs.

Ø
There are not many cases where function return values are passed by reference. They are generally used only in class assignment functions for chained expression.

11.1.3const member functions

Any function that does not modify data members should be declared as the const type. If you accidentally modify a data member or call a non-const member function when writing a const member function, an error is returned during compilation. The statement format is as follows:

Class Stack

{

Public:

Int POP (void );

Int getcount (void) const; // const member function

PRIVATE:

Int m_num;

};

Int Stack: getcount (void) const

{

++ M_num; // compilation Error

Pop (); // compilation Error

Return m_num;

}

 

Conclusion: const is used to modify function parameters, return values, and function bodies.

There are three transmission methods for parameters and return values: pointer transfer, value transfer, and reference transfer.

Parameter: it is meaningless to modify the output parameters by using Const. Only the input parameters are modified. Const pointer transfer: prevents modification of input parameters; value transfer: const modification is meaningless; const reference transfer: useful for non-Internal data parameters, avoids the generation of temporary objects, and improves efficiency.

Returned value: const pointer transmission: The returned value cannot be modified and can only be assigned to the const pointer of the same type. Value transmission: To generate a temporary object, const modification is meaningless. const reference transmission: it is useful for non-Internal data and is generally only used in class assignment functions. Pay attention to pointer transmission and reference transfer. The return value should not point to a local variable.

Function body itself: it is declared as a const member function without modifying the data member. It cannot change the data member or call a non-const member function.

Differentiation: pointer to constant const int * P; pointer constant int * const P ;.

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.