非引用参数的const [英] const for non-reference arguments

查看:75
本文介绍了非引用参数的const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有此代码:

void Foo(aBasicType aIn) //Where aBasicType is int, char etc.
{
    //...
}

是否有使其成为 const aBasicType 的重点,因为无论如何都要复制它?我问的原因之一是因为我已经在第三方代码中看到它,并且想知道是否有我不知道的东西。

Is there any point in making it const aBasicType since it is going to be copied anyway? One of the reasons I am asking is because I have seen it in 3rd party code and was wondering if there is something I am not aware of.

推荐答案

如果您知道函数在执行过程中不需要修改其值,则声明为 const 不会很麻烦。

It cannot hurt to declare it const if you know that your function needs not modify its value during execution.

请注意,当参数按值传递时,更改其参数的函数应该很少。

Note that functions that change their arguments, when arguments are passed by value, should be rare.

声明变量 const 可以阻止您编写 if(aIn = someValue)

Declaring your variable const can prevent you from writing if (aIn = someValue).

这篇关于非引用参数的const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆