C++ const用法

C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的。如果在编程中确实有某个值保持不变,就应该明确使用const,这样可以获得编译器的帮助。

1.const 修饰变量 

#include<iostream>
using namespace std;
int main(){
    int a1=3;   ///non-const data
    const int a2=a1;    ///const data

    int * a3 = &a1;   ///non-const data,non-const pointer
    const int * a4 = &a1;   ///const data,non-const pointer
    int * const a5 = &a1;   ///non-const data,const pointer
    int const * const a6 = &a1;   ///const data,const pointer
    const int * const a7 = &a1;   ///const data,const pointer

    return 0;
}

const修饰指针变量时:

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

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

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

2.const修饰函数参数

  如果实参与引用参数不匹配,C++将生成临时变量。使用const引用使函数能够正确生成并使用临时变量。

Double refcube(const double& ra)
{
     return ra*ra*ra;
}
 
double side = 3.0;
double* pd = &side;
double& rd = side;
long edge = 5L;
double lens[4]={2.3,3.4,4.5,6.7};
double c1 = refcube(side); // ra 是side
double c2 = refcube(lens[2]); // ra是lens[2]
double c3 = refcube(rd);  // ra 是 rd
double c4 = refcube(*pd); // ra 是*pd
double c5 = refcube(edge); // ra 是临时变量
double c6 = refcube(7.0); // ra 是临时变量
double c7 = refcube(side+10.0); // ra 是临时变量

3.const修饰成员变量

m_len变量不允许修改,所以用 const 进行了限制,这样就只能使用初始化列表来进行赋值

class VLA{
public:
    VLA(int len);  //构造函数
    ~VLA();  //析构函数
public:
    void input();  //从控制台输入数组元素
    void show();  //显示数组元素
private:
    int* at(int i);  //获取第i个元素的指针
private:
    const int m_len;  //数组长度
    int *m_arr; //数组指针
    int *m_p;  //指向数组第i个元素的指针
};

VLA::VLA(int len): m_len(len){  //使用初始化列表来给 m_len 赋值
    if(len > 0){ m_arr = new int[len];  /*分配内存*/ }
    else{ m_arr = NULL; }
}

4.const修饰成员函数

(1)const修饰的成员函数不能修改任何的成员变量(mutable修饰的变量除外)

(2)const成员函数不能调用非const成员函数,因为非const成员函数可以会修改成员变量

(3)需要强调的是,必须在成员函数的声明和定义处同时加上 const 关键字,也就是第一种方式。

5.const修饰成员对象

point、point1 分别是常对象以及常对象指针,它们都只能调用 const 成员函数。

#include <iostream>
using namespace std;

class Point {
public:
	Point(int _x) :x(_x) {}
	// 第一种方式
	int getX() const;
	// 第二种方式
	void testConstFunction(int _x) const {

		// 错误,在const成员函数中,不能修改任何类成员变量
		//x = _x;

		// 错误,const成员函数不能调用非const成员函数,因为非const成员函数可以会修改成员变量
		//modify_x(_x);
	}

	void modify_x(int _x) {
		x = _x;
	}
private:
	int x;
};

int Point::getX() const {
	return x;
}

int main()
{
	const Point point(10);
	// 错误,只能调用 const 成员函数
	//point.modify_x(3);
	cout << point.getX() << endl;// 输出10

	const Point* point1 = new Point(100);
	// 错误,只能调用 const 成员函数
	//point1->modify_x(3);
	cout << point1->getX() << endl;// 输出10

	getchar();
	return 0;
}

 6.const修饰函数返回值

(1)指针传递

如果返回const data,non-const pointer,返回值也必须赋给const data,non-const pointer。因为指针指向的数据是常量不能修改。

const int * mallocA(){  ///const data,non-const pointer
    int *a=new int(2);
    return a;
}

int main()
{
    const int *a = mallocA();
    ///int *b = mallocA();  ///编译错误
    return 0;
}

(2)值传递

 如果函数返回值采用“值传递方式”,由于函数会把返回值复制到外部临时的存储单元中,加const 修饰没有任何价值。所以,对于值传递来说,加const没有太多意义。

所以:

  不要把函数int GetInt(void) 写成const int GetInt(void)。
  不要把函数A GetA(void) 写成const A GetA(void),其中A 为用户自定义的数据类型。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值