C++ 指针

“左定值,右定向,const修饰不变量”
int    *ip;    /* 一个整型的指针 */

指针是一个变量, ip是指针变量的名称,变量地址,*表示该变量是一个指针类型。

int var = 20; // 实际变量的声明 int *ip;// 指针变量的声明 ip = &var; // 在指针变量中存储 var 的地址

或者通过int *p=&var,将变量的地址赋值给p指针

此时*ip=20

A:----------------------------------------------------------------------------------------------------------

const int *p=8; //*位于const右边,8的值不能改变

例如:

const int  a = 7;
int *p=(int *)&a; // 取出a的地址,转化为整型指针,赋值给p指针

*p=8;cout<<a;//此时a的值依旧为7

想要改变a的值,可在前面加上volatile关键字,如:

volatile const int  a = 7;
B:------------------------------------------------------------------------------------------------------------

//对于const指针p其指向的内存地址不能够被改变,但其内容可以改变。简称,右定向。因为const位于*号的右边

例如:

int a=8;int* const p=&a;*p=8; //it is right

int b=7; p=&b;//it is wrong

C:------------------------------------------------------------------------------------------------------------

将A和B合并

int a=8; const int * const p=&a;

//这时,const p的指向的内容和指向的内存地址都已固定,不可改变。

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

 

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

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

复制代码
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 void Cpf(const int a)
 6 
 7 {
 8 
 9     cout<<a;
10 
11     // ++a;  it's wrong, a can't is changed
12 
13 }
14 
15 int main(void)
16 
17 {
18 
19     Cpf(8);
20 
21     system("pause");
22 
23     return 0;
24 
25 }
复制代码

 

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修饰返回值分三种情况。

A:const修饰内置类型的返回值,修饰与不修饰返回值作用一样。

复制代码
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 const int Cmf()
 6 
 7 {
 8 
 9     return 1;
10 
11 }
12 
13 int Cpf()
14 
15 {
16 
17     return 0;
18 
19 }
20 
21 int main(void)
22 
23 {
24 
25     int _m = Cmf();
26 
27     int _n = Cpf();
28 
29  
30 
31     cout<<_m<<" "<<_n;
32 
33     system("pause");
34 
35     return 0;
36 
37 }
复制代码

 

B:const 修饰自定义类型的作为返回值,此时返回的值不能作为左值使用,既不能被赋值,也不能被修改。

 

C: const 修饰返回的指针或者引用,是否返回一个指向const的指针,取决于我们想让用户干什么。

 

 

四、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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值