[英]What does 'const' do in operator() overloading?
I have a code base, in which for Matrix class, these two definitions are there for ()
operator: 我有一个代码库,在Matrix类中,这两个定义适用于
()
运算符:
template <class T> T& Matrix<T>::operator() (unsigned row, unsigned col)
{
......
}
template <class T> T Matrix<T>::operator() (unsigned row, unsigned col) const
{
......
}
One thing I understand is that the second one does not return the reference but what does const
mean in the second declaration? 我理解的一件事是,第二个不返回引用,但
const
在第二个声明中意味着什么? Also which function is called when I do say mat(i,j)
? 当我说
mat(i,j)
时,还调用了哪个函数?
Which function is called depends on whether the instance is const or not. 调用哪个函数取决于实例是否为const。 The first version allows you to modify the instance:
第一个版本允许您修改实例:
Matrix<int> matrix;
matrix(0, 0) = 10;
The const overload allows read-only access if you have a const instance (reference) of Matrix: 如果你有一个Matrix的const实例(引用),const重载允许只读访问:
void foo(const Matrix<int>& m)
{
int i = m(0, 0);
//...
//m(1, 2) = 4; //won't compile
}
The second one doesn't return a reference since the intention is to disallow modifying the object (you get a copy of the value and therefore can't modify the matrix instance). 第二个不返回引用,因为意图是禁止修改对象(您获得值的副本,因此无法修改矩阵实例)。
Here T is supposed to be a simple numeric type which is cheap(er) to return by value. 这里T应该是一个简单的数字类型,便宜(呃)按值返回。 If T might also be a more complex user-defined type, it would also be common for const overloads to return a const reference:
如果T也可能是一个更复杂的用户定义类型,那么const重载通常会返回一个const引用:
template <class T>
class MyContainer
{
//..,
T& operator[](size_t);
const T& operator[](size_t) const;
}
The const version will be called on const Matrices. const版本将在const Matrices上调用。 On non-const matrices the non-const version will be called.
在非const矩阵上,将调用非const版本。
Matrix<int> M;
int i = M(1,2); // Calls non-const version since M is not const
M(1,2) = 7; // Calls non-const version since M is not const
const Matrix<int> MConst;
int j = MConst(1,2); // Calls const version since MConst is const
MConst(1,2) = 4; // Calls the const version since MConst is const.
// Probably shouldn't compile .. but might since return value is
// T not const T.
int get_first( const Matrix<int> & m )
{
return m(0,0); // Calls the const version as m is const reference
}
int set_first( Matrix<int> & m )
{
m(0,0) = 1; // Calls the non-const version as m is not const
}
Which function is called depends on whether the object is const
. 调用哪个函数取决于对象是否为
const
。 For const
objects const
overload is called: 对于
const
对象,调用const
重载:
const Matrix<...> mat;
const Matrix<...>& matRef = mat;
mat( i, j);//const overload is called;
matRef(i, j); //const overloadis called
Matrix<...> mat2;
mat2(i,j);//non-const is called
Matrix<...>& mat2Ref = mat2;
mat2Ref(i,j);//non-const is called
const Matrix<...>& mat2ConstRef = mat2;
mat2ConstRef(i,j);// const is called
The same applies to pointers. 这同样适用于指针。 If the call is done via a pointer-to-const, a const overload is called.
如果通过指向const的指针完成调用,则调用const重载。 Otherwise a non-const overload is called.
否则调用非const重载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.