c++const复习总结

const 是constant的缩写,本意是不变的,不易改变的意思。

const 在C++中是用来修饰内置类型变量,自定义对象,成员函数,返回值,函数参数


一、const修饰普通类型的变量。

直接修饰普通常量没有什么要点问题。

二、

在修饰指针变量时:

const 修饰指针变量有以下三种情况。

A:const 修饰指针指向的内容,则内容为不可变量。

B:const 修饰指针,则指针为不可变量。

C:const 修饰指针和指针指向的内容,则指针和指针指向的内容都为不可变量。

  (1)只有一个const,如果const位于*左侧,表示指针所指数据是常量,不能通过解引用修改该数据;指针本身是变量,可以指向其他的内存单元。

    如:const int *p = 8; 

因为const位于*号的左边


(2)只有一个const,如果const位于*右侧,表示指针本身是常量,不能指向其他内存地址;指针所指的数据可以通过解引用修改。

 如:

复制代码
1  int a = 8;
2  
3  int* const p = &a;
4 
5  *p = 9; //it’s right
6 
7  int  b = 7;
8 
9  p = &b; //it’s wrong
复制代码

因为const位于*号的右边


(3)两 *左右各一个,表示指针和指针所指数据都不能修改。

如:

1 int a = 8;
2 
3 const int * const  p = &a;
4 
5  

三、const参数传递和函数返回值。

 

对于const修饰函数参数可以分为三种情况。

(1):值传递的const修饰传递,一般这种情况不需要const修饰,因为函数会自动产生临时变量复制实参值。

(2):

B:当const参数为指针时,可以防止指针被意外篡改。

复制代码
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 void Cpf(int *const a)
 6 
 7 {
 8 
 9     cout<<*a<<" ";
10 
11     *a = 9;
12 
13 }
14 
15 int main(void)
16 
17 {
18 
19     int a = 8;
20 
21     Cpf(&a);
22 
23     cout<<a; // a is 9
24 
25     system("pause");
26 
27     return 0;
28 
29 }
复制代码


C:自定义类型的参数传递,需要临时对象复制参数,对于临时对象的构造,需要调用构造函数,比较浪费时间,因此我们采取const外加引用传递的方法。

并且对于一般的int ,double等内置类型,我们不采用引用的传递方式。

复制代码
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 class Test
 6 
 7 {
 8 
 9 public:
10 
11     Test(){}
12 
13     Test(int _m):_cm(_m){}
14 
15     int get_cm()const
16 
17     {
18 
19        return _cm;
20 
21     }
22 
23 private:
24 
25     int _cm;
26 
27 };
28 
29  
30 
31 void Cmf(const Test& _tt)
32 
33 {
34 
35     cout<<_tt.get_cm();
36 
37 }
38 
39 int main(void)
40 
41 {
42 
43     Test t(8);
44 
45     Cmf(t);
46 
47     system("pause");
48 
49     return 0;
50 
51 }
复制代码

//结果输出 8


四、const修饰类成员函数.

const 修饰类成员函数,其目的是防止成员函数修改被调用对象的值,如果我们不想修改一个调用对象的值,所有的成员函数都应当声明为const成员函数。注意:const关键字不能与static关键字同时使用,因为static关键字修饰静态成员函数,静态成员函数不含有this指针,即不能实例化,const成员函数必须具体到某一实例。

 

下面的get_cm()const;函数用到了const成员函数

 

复制代码
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 class Test
 6 
 7 {
 8 
 9 public:
10 
11     Test(){}
12 
13     Test(int _m):_cm(_m){}
14 
15     int get_cm()const
16 
17     {
18 
19        return _cm;
20 
21     }
22 
23 private:
24 
25     int _cm;
26 
27 };
28 
29  
30 
31 void Cmf(const Test& _tt)
32 
33 {
34 
35     cout<<_tt.get_cm();
36 
37 }
38 
39 int main(void)
40 
41 {
42 
43     Test t(8);
44 
45     Cmf(t);
46 
47     system("pause");
48 
49     return 0;
50 
51 }
复制代码

 

如果get_cm()去掉const修饰,则Cmf传递的const _tt即使没有改变对象的值,编译器也认为函数会改变对象的值,所以我们尽量按照要求将所有的不需要改变对象内容的函数都作为const成员函数。

 

 如果有个成员函数想修改对象中的某一个成员怎么办?这时我们可以使用mutable关键字修饰这个成员,mutable的意思也是易变的,容易改变的意思,被mutable关键字修饰的成员可以处于不断变化中,如下面的例子。

 

复制代码
 1 #include<iostream>
 2 using namespace std;
 3 class Test
 4 {
 5 public:
 6     Test(int _m,int _t):_cm(_m),_ct(_t){}
 7     void Kf()const
 8     {
 9         ++_cm; //it's wrong
10         ++_ct; //it's right
11     }
12 private:
13     int _cm;
14     mutable int _ct;
15 };
16 
17 int main(void)
18 {
19     Test t(8,7);
20     return 0;
21 }
复制代码

这里我们在Kf()const中通过++_ct;修改_ct的值,但是通过++_cm修改_cm则会报错。因为++_cm没有用mutable修饰




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值