CPP about const and functions

Source: Internet
Author: User

void Fun () const{}; , const void Fun () {}; and void const fun () {}; The difference?

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

If the return value of the function "by address" is added with the const modifier, the contents of the function return value (that is, the address) cannot be modified, and the return value can only be assigned to the same type of pointer with the const modifier.

If the return value of a function by value is added with the const modifier, the const modifier has no value because the function copies the return value to an external temporary storage cell.

So don't try not to put int fun2 (); write a const int fun2 (); Because it doesn't make sense.

Cases:

#include <iostream>

using namespace Std;

int num=10; Global variables

const int *FUN1 () {//Delivered by address

Return &num; return address

}

const int FUN2 () {///By value/best Write int fun2 directly ()

return num;

}

int main ()

{

const int *FUN1 ();

int *t1=fun1 (); Error, must be a const type

const int *T1=FUN1 ();

*t1=20; By address, you cannot modify the value that it points to a variable or constant

cout<< "Const int *fun1 (): T" <<*t1<<endl;

const int fun2 (); It is best to declare the int fun2 directly ()

int t2=fun2 (); Non-const variable can change function return value

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 member function of a class is appended with a const, indicating that the function cannot make any changes to the data members of this class object (precisely, non-static data members).

Cases:

#include <iostream>

using namespace Std;

Class R

{

Public

R (): NUM1 (1) {}

int sum1 (int a) const

{

num1=10; Error, non-static data members cannot be modified

return A+NUM1;

}

int sum2 (int a) const

{

num2=2; Correct, modifying static data members

return a+num2;

}

int sum3 (int a)//No const

{

num1=10; Correct, modifying non-static data members

num2=20; Correct, modifying static data members

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.