[英]Is there a way to declare that input parameter of the function must be const reference?
motivation for this is that (rarely)I have a need to know that the input parameter of the class constructor or function in general is const. 这样做的动机是(很少)我需要知道类构造函数或函数的输入参数通常是const。 Usually when the class is a helper that "automates" some procedure.
通常,当该类是一个使某些过程“自动化”的助手时。
Example: 例:
Is this the OK scoped way to get random element from the container? 这是从容器中获取随机元素的确定范围的方法吗?
If you look at the code it is clear that if the container passed to constructor is changed later class functionality is broken. 如果您看一下代码,很明显,如果传递给构造函数的容器被更改,则类功能将被破坏。 So is there a way to have a function "demand" const instead of "promise" const.
因此,有一种方法可以使用函数“ demand” const而不是“ promise” const。
Example: 例:
int f(const vector<int>& v)
{
return v.size();
}
int main()
{
vector<int> v;
v.push_back(42); // can f be changed to reject v because it is not const
cout << f(v);
}
Declare but do not implement a non-const version. 声明但不实现非const版本。
int f(vector<int>& v);
Attempts to pass a non-const vector will be resolved to this function, and then you will get a linker error because no such function exists. 尝试通过非常量向量的尝试将被解析为该函数,然后您将收到链接器错误,因为不存在此类函数。
Some fancy template games may be able to turn it into a compile-time error. 一些精美的模板游戏可能能够将其转换为编译时错误。
There is no way to guarantee that. 没有办法保证这一点。 If you want to be sure the object doesn't change, it should be owned by the class.
如果要确保对象不变,则该对象应归类所有。 One way to accomplish that is to copy the object you want to keep constant into the class, or if using C++11 to let the constructor take an rvalue-reference, and move the object.
一种实现方法是将要保持常数的对象复制到类中,或者使用C ++ 11让构造函数获取右值引用并移动对象。
If you want some attributes inside class be safe from any external changes they should be copied by constructor to attribute const TypeName _attr;
如果希望类中的某些属性不受任何外部更改的影响,则应将它们由构造
const TypeName _attr;
复制到属性const TypeName _attr;
. 。
I don't think, it is possible. 我不认为有可能。
If it would be possible, you could still do like that 如果有可能,您仍然可以那样做
Object mutable_;
const Object & notMutavle = mutable_;
func(notMutable);
mutable.change();
Anyway, even const
object's state can be possibly changed, if it has some mutable
feilds. 无论如何,即使
const
对象的状态也可以更改,如果它具有一些mutable
字段。
If you want to be sure, that nobody changes your object, you should manage it yourself. 如果要确保没有人更改对象,则应该自己进行管理。
You can do it by overloading the function for non-const reference. 您可以通过重载非常量引用的函数来实现。
Next example demonstrates this. 下一个示例对此进行了演示。 However, instead of returning -1, the first version of f should throw an exception.
但是,代替返回-1,f的第一个版本应该引发异常。
#include <vector>
#include <iostream>
int f(std::vector<int>& )
{
return -1;
}
int f(const std::vector<int>& v)
{
return v.size();
}
int main()
{
std::vector<int> v;
v.push_back(42); // can f be changed to reject v because it is not const
std::cout << f(v)<<std::endl;
const std::vector<int> cv(5);
std::cout << f(cv)<<std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.