Cpp about const and functions

Source: Internet
Author: User

What is the difference between void fun () const {};, const void fun () {}; and void const fun?

A: const void fun () {}; and void const fun () {}; are the same.

If you add const to the return value of the "by-Address Transfer Method" function, the content of the function return value (that is, the address) cannot be modified, the returned value can only be assigned to the same type pointer with const modifier.

If you use the "pass by value" function to add const modification to the return value, because the function will copy the return value to an external temporary storage unit, adding const modification has no value.

Therefore, do not write int fun2 (); As const int fun2 (); it is meaningless.

Example:

# Include <iostream>

Using namespace std;

Int num = 10; // global variable

Const int * fun1 () {// pass by address

Return & num; // return address

}

Const int fun2 () {// pass by value // it is best to directly write int fun2 ()

Return num;

}

Int main ()

{

Const int * fun1 ();

// Int * t1 = fun1 (); // error, must be of the const type

Const int * t1 = fun1 ();

// * T1 = 20; // pass by address. You cannot modify the value pointing to a variable or constant.

Cout <"const int * fun1 (): t" <* t1 <endl;

Const int fun2 (); // it is best to declare int fun2 () directly ()

Int t2 = fun2 (); // the return value of the function can be changed for non-const variables.

Const int t3 = fun2 ();

T2 + = 10; // pass by value. You can modify the return value.

Cout <"const int fun2 (): t" <t2 <endl;

Return 0;

}

Void fun () const {};

The const is added to the member function of the class, indicating that this function cannot make any changes to the data member of the Class Object (which is not a static data member accurately.

Example:

# Include <iostream>

Using namespace std;

Class R

{

Public:

R (): num1 (1 ){}

Int sum1 (int a) const

{

// Num1 = 10; // error. The non-static data member cannot be modified.

Return a + num1;

}

Int sum2 (int a) const

{

Num2 = 2; // correct. Modify the static data member.

Return a + num2;

}

Int sum3 (int a) // No const

{

Num1 = 10; // correct. Modify non-static data members.

Num2 = 20; // correct. Modify the static data member.

Return a + num1 + num2;

}

Private:

Int num1;

Static int num2;

};

Int R: num2 = 0;

Int main ()

{

Cout <"t. sum1 (1): t" <t. sum1 (1) <endl;

Cout <"t. sum2 (1): t" <t. sum2 (1) <endl;

Cout <"t. sum3 (1): t" <t. sum3 (1) <endl;

Return 0;

}

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.