const 是constant的缩写,本意是不变的,不易改变的意思。html
const 在C++中是用来修饰内置类型变量,自定义对象,成员函数,返回值,函数参数。ios
// for variable const int a = 8; int *p = (int *)&a; *p =7; // wrong, it's unpredictable // for pointer, 左定值,右定向 int a = 8; const int *p = &a; *p = 7; // wrong int * const p2 = &a; int b = 10; p2 = &b; // wrong // for func param class A {}; void func(const A *a){} // equal to void func(const A& a){}, improve performance for copy constructor comparing to void func(A a) void func(A * const a){} // equal to void func(A& a){} // for class method, indicate 'the method can't modify the instance data', (mutable修饰的变量除外) // 不能调用非const成员函数 class B{ private: int count; public: int GetCount() const { count ++; // wrong return count; } } // for return value, no practical value
以下:函数
const int a = 7; int b = a; //it's right a = 8; // it's wrong,
a被定义为一个常量,而且能够将a赋值给b,可是不能给a再次赋值。对一个常量赋值是违法的事情,由于a被编译器认为是一个常量,其值不容许修改。优化
接着看以下的操做:this
#include<iostream> using namespace std; int main(void) { const int a = 7; int *p = (int*)&a; *p = 8; cout<<a; system("pause"); return 0; }
对于const变量a,咱们取变量的地址并转换赋值给 指向int的指针,而后利用*p = 8;从新对变量a地址内的值赋值,而后输出查看a的值。spa
从下面的调试窗口看到a的值被改变为8,可是输出的结果仍然是7。3d
从结果中咱们能够看到,编译器而后认为a的值为一开始定义的7,因此对const a的操做就会产生上面的状况。因此千万不要轻易对const变量设法赋值,这会产生意想不到的行为。指针
若是不想让编译器察觉到上面到对const的操做,咱们能够在const前面加上volatile关键字调试
Volatile关键字跟const对应相反,是易变的,容易改变的意思。因此不会被编译器优化,编译器也就不会改变对a变量的操做。code
#include<iostream> using namespace std; int main(void) { volatile const int a = 7; int *p = (int*)&a; *p = 8; cout<<a; system("pause"); return 0; }
输出结果如咱们指望的是8
const 修饰指针变量有如下三种状况。
对于A:
const int *p = 8;
则指针指向的内容8不可改变。简称左定值,由于const位于*号的左边。
对于B:
int a = 8; int* const p = &a; *p = 9; //it’s right int b = 7; p = &b; //it’s wrong
对于const指针p其指向的内存地址不可以被改变,但其内容能够改变。简称,右定向。由于const位于*号的右边。
对于C:
则是A和B的合并,
int a = 8; const int * const p = &a;
这时,const p的指向的内容和指向的内存地址都已固定,不可改变。
对于A,B,C三种状况,根据const位于*号的位置不一样,我总结三句话便于记忆的话,
左定值,右定向,const修饰不变量。
对于const修饰函数参数能够分为三种状况。
A:值传递的const修饰传递,通常这种状况不须要const修饰,由于函数会自动产生临时变量复制实参值。
#include<iostream> using namespace std; void Cpf(const int a) { cout<<a; // ++a; it's wrong, a can't is changed } int main(void) { Cpf(8); system("pause"); return 0; }
B:当const参数为指针时,能够防止指针被意外篡改。
#include<iostream> using namespace std; void Cpf(int *const a) { cout<<*a<<" "; *a = 9; } int main(void) { int a = 8; Cpf(&a); cout<<a; // a is 9 system("pause"); return 0; }
C:自定义类型的参数传递,须要临时对象复制参数,对于临时对象的构造,须要调用构造函数,比较浪费时间,所以咱们采起const外加引用传递的方法。
而且对于通常的int ,double等内置类型,咱们不采用引用的传递方式。
#include<iostream> using namespace std; class Test { public: Test(){} Test(int _m):_cm(_m){} int get_cm() const { return _cm; } private: int _cm; }; void Cmf(const Test& _tt) { cout<<_tt.get_cm(); } int main(void) { Test t(8); Cmf(t); system("pause"); return 0; }
结果输出 8
Const修饰返回值分三种状况。
A:const修饰内置类型的返回值,修饰与不修饰返回值做用同样。
#include<iostream> using namespace std; const int Cmf() { return 1; } int Cpf() { return 0; } int main(void) { int _m = Cmf(); int _n = Cpf(); cout<<_m<<" "<<_n; system("pause"); return 0; }
B:const 修饰自定义类型的做为返回值,此时返回的值不能做为左值使用,既不能被赋值,也不能被修改。
C: const 修饰返回的指针或者引用,是否返回一个指向const的指针,取决于咱们想让用户干什么。
const 修饰类成员函数,其目的是防止成员函数修改被调用对象的值,若是咱们不想修改一个调用对象的值,全部的成员函数都应当声明为const成员函数。注意:const关键字不能与static关键字同时使用,由于static关键字修饰静态成员函数,静态成员函数不含有this指针,即不能实例化,const成员函数必须具体到某一实例。
下面的get_cm() const
;函数用到了const成员函数
#include<iostream> using namespace std; class Test { public: Test(){} Test(int _m):_cm(_m){} int get_cm()const { return _cm; } private: int _cm; }; void Cmf(const Test& _tt) { cout<<_tt.get_cm(); } int main(void) { Test t(8); Cmf(t); system("pause"); return 0; }
若是get_cm()去掉const修饰,则Cmf传递的const _tt即便没有改变对象的值,编译器也认为函数会改变对象的值,因此咱们尽可能按照要求将全部的不须要改变对象内容的函数都做为const成员函数。
若是有个成员函数想修改对象中的某一个成员怎么办?这时咱们可使用mutable关键字修饰这个成员,mutable的意思也是易变的,容易改变的意思,被mutable关键字修饰的成员能够处于不断变化中,以下面的例子。
#include<iostream> using namespace std; class Test { public: Test(int _m,int _t):_cm(_m),_ct(_t){} void Kf() const { ++_cm; //it's wrong ++_ct; //it's right } private: int _cm; mutable int _ct; }; int main(void) { Test t(8,7); return 0; }
这里咱们在Kf()const中经过++_ct;修改_ct的值,可是经过++_cm修改_cm则会报错。由于++_cm没有用mutable修饰。
转载:
C++ const用法 尽量使用const http://www.javashuo.com/article/p-bqwkndqd-cd.html
C++const 关键字小结 https://www.cnblogs.com/Forever-Kenlen-Ja/p/3776991.html