const 常量_我在项目中使用c++ const

记得读Effective c++时,有一个tips,尽可能多的使用const(Use const whenever possible)

我在项目中用的比较多的情况有:

  1. 定义常量对象
  2. 基于范围的 for 循环我一般使用常量引用的方式(不修改元素情况)
  3. 配合const+& 做参数传递 const parameter
  4. const函数 const function
  5. const返回值 const return value
  6. const 修饰指针

1.常量对象定义

#define PI=3.1415926;
const double PI_ = 3.1415926;

2. 基于范围的for循环我一般使用常量引用的方式(不修改元素)

	std::vector<int> nums{ 1,2,3,4 };
	int sum = 0;
	for(const auto& num:nums){
		sum += num;
	}

3. 函数传递时我喜欢用引用(有的人喜欢用指针)

我通过加不加const来标示,

  • 对于不修改的情况下通过const+&的方式
int calVecSum(const std::vector<int>& nums){
	int sum = 0;
	for (const auto& num : nums) {
		sum += num;
	}
	return sum;
}
  • 通过参数作为返回值时,通过引用
void calVecSum(const std::vector<int>& nums,int& sumOut){
	for (const auto& num : nums) {
		sumOut += num;
	}
}

//当然也可以用指针,我比较喜欢用引用
void calVecSum1(const std::vector<int>& nums, int* sumOut) {
	for (const auto& num : nums) {
		*sumOut += num;
	}
}

const 再传递参数时不和&配合的话,跟不加没区别

//加不加const的意思是一样的
	void SetAge(int a) {age = a;}	
	void SetAge(const int a) {age = a;}
        //error: has already been defined

4.const 函数

const funtion means this function will note change any of the member variabes of this class

有点类似c# 的get属性器,增加const后,不修改成员变量数据,也不能调用非const函数,提高程序的健壮性

  • note:const成员函数可以调用静态成员变量及静态成员函数
class CPoint2D{
public:
	CPoint2D():x(0),y(0){}
	static int index;
	double x;
	double y;
	void SetX(double value) { x = value; }

	double GetX() const { 
		y = 3    ;  //错误,修改成员变量
		SetX(3)  ;  //错误,调用非const函数
		index = 1;  //ok
		return x;
	}
	double GetY() const { return y; }
};

5. const return value

  • 注意下文函数中去掉&后,const就完全没有作用了, because the name is returned by value. what the function returned is copy of name which is a temporary
class Dog {
public:
	std::string name;
	//const return value
	const std::string& getName() {return name;}
        //注意去掉&
        const std::string getNanme{return name;}

};

6. const with pointer

  • if const is on the left of*, data is const
  • if const is on the right of *,pointer is const
	int i = 9;
	const int *p1 = &i;          //data is const, pointer is not
	int* const p2=new int;       //pointer is const, data is not
	const int* const p3=new int; //data and pointer are both const

下面是一样的

    // there are the same ,they are both on the left of *
    int const *P4=&i;
    const int *p4=&i;

const 好处

 // const has several benefits
    // a.) Guards against inadvertent write to the variable, so it can stop the wrong behavior at
    // b.) Const is a way of self documenting, tell the reader this value will not be changed
    // c.) Enables compiler to do more optimization, make the code tighter & faster
    // d.) const means the variable can be put in rom (read-only memory) 

待续...

参考:

默然:C++笔记 · C++关键字 - const​zhuanlan.zhihu.com
f947d61a43dab253fbd7584dcff53ec7.png
小林coding:C++ const常量对象、常量成员函数和常引用​zhuanlan.zhihu.com
af1b08519d61717f70a4b867e1faf0a7.png
https://www.youtube.com/watch?v=RC7uE_wl1Uc&list=PLE28375D4AC946CC3&index=2​www.youtube.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值