Const Summary: Do function parameters, do function return value, and const function

Source: Internet
Author: User

To do function parameters:

For example, the value of the fun (const int * i) parameter I cannot be modified during the fun () function execution, and it will keep the value passed in when this function is called. If the statement attempting to modify the I value appears in the Fun () function, it will cause the program to fail to compile, thus protecting the parameter I.

Note: const is commonly used to restrict pointer parameters to functions, reference and array parameters, and general form parameters because the parameter and argument are not variables of the same memory unit, so modifications to the formal parameters do not affect the arguments, so there is no need to restrict the function body from modifying the parameters .  

To do a function return value:

The function return value is const only if the function is returned as a reference. The reference to the function return value is reference to the relevant knowledge here

A function return value reference constant indicates that a function call expression cannot be used as an lvalue .

For example, Function min ().

int & min (int &i, int &j);

You can assign a value to a function call because it returns an lvalue: Min (A, b) = 4;

However, if the return value of the function is limited to const: const int & min (int & i, int &j);

Then, the Min (A, B) call cannot be assigned.

Const function:

In a class, you can define the following form of member functions for a class:

Class ClassName {

int member;

Public

int getmember () const;

};

Here, the const that is appended to the function definition header indicates that the function is a "read-only function" that can be referenced, but cannot alter the data members in this class.

If this is not the case in the function body:

ClassName:: GetMember ()

{member = 4;

return member;

}

In addition, theconst member function cannot invoke other non-const functions in the function.

For a more detailed reference here


Summarize:

At first just want to understand the const do function parameters, see more than half an hour, the final summary of a bit, in many of the code to explain the const, although not difficult to understand, but it is difficult to remember, at this time can not be anxious, do not have to hard to remember, just hit a face-to-face, in the practical application in the

Const Summary: Do function parameters, do function return value, and const function

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.