Const usage in C ++

Source: Internet
Author: User

Constants are constant constants that provide security and controllability in C ++ programming. A constant is identified by const. One of its functions is to replace macro replacement. Const provides a strict type check for values. Compared with macro replacement, const provides powerful security assurance.

 

Const is usually used to modify the function parameters as pointers and references, and can only modify the input parameters. If the input parameters use the value transfer method, because temporary variables are automatically generated during function calling for copying real parameters, the original parameters are not modified, so you do not need to use const for modification.

 

The cosnt-modified part of the function body is constant. If the parameter is const type * parameter, the content of the passed pointer cannot be modified, this method is used to protect the content pointed to by the original pointer. If the parameter is const
Type & parameter, the passed referenced object cannot be changed, that is, the object protects the source object.

 

We recommend that you do not use the value transfer method for non-Internal data type input parameters, but use the "const
Reference object "transfer method, to improve efficiency. If the input parameters are of the internal data type, we recommend that you do not change the value TRANSMISSION METHOD
Reference transfer mode.

 

The following describes how the function return value is of the const type.

If the return value of a function is a constant, it means that the original variable cannot be modified. If the return value is used, the return value is an internal data type. This function declaration is meaningless.

As follows:

Int function0 ()

{Return 0 ;}

 

Const int function1 ()

{Return 0 ;}

 

Void main ()

{

Const int A = function0 ();

Int B = function1 ();

}

Compile and run the above program. It can be seen that for internal data, whether the returned value is a cosnt has no effect, because this function declaration has no significance, this is not recommended. For non-Internal data types, the situation is different. If a Class Object const constant is returned by a function, the return value is protected, that is, the return value cannot be used as the left value.

 

Const can be used to declare a class member function, as follows:

Void function () const {}

The modified member functions cannot modify member variables. If a statement modifies a member variable, compilation errors will occur, greatly improving the robustness of the program.

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.