‘const’ 是什么意思?在operator()中重载吗?

发布于 2024-08-26 21:43:24 字数 400 浏览 3 评论 0原文

我有一个代码库,其中对于 Matrix 类,() 运算符有这两个定义:

template <class T> T& Matrix<T>::operator() (unsigned row, unsigned col)
{
    ......
}


template <class T> T Matrix<T>::operator() (unsigned row, unsigned col) const
{
    ......
}

我理解的一件事是,第二个定义不返回引用,但是 const 是什么? 在第二个声明中意味着什么?另外,当我说 mat(i,j) 时会调用哪个函数?

I have a code base, in which for Matrix class, these two definitions are there for () operator:

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? Also which function is called when I do say mat(i,j)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呢古 2024-09-02 21:43:24

调用哪个函数取决于实例是否为 const。第一个版本允许您修改实例:

 Matrix<int> matrix;
 matrix(0, 0) = 10;

如果您有 Matrix 的 const 实例(引用),则 const 重载允许只读访问:

 void foo(const Matrix<int>& m)
 {
     int i = m(0, 0);
     //...
     //m(1, 2) = 4; //won't compile
 }

第二个版本不返回引用,因为其目的是禁止修改对象(您获取值的副本,因此无法修改矩阵实例)。

这里 T 应该是一个简单的数字类型,按值返回很便宜。如果 T 也可能是更复杂的用户定义类型,则 const 重载返回 const 引用也很常见:

 template <class T>
 class MyContainer
 {
      //..,
      T& operator[](size_t);
      const T& operator[](size_t) const;
 }

Which function is called depends on whether the instance is const or not. 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:

 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. If T might also be a more complex user-defined type, it would also be common for const overloads to return a const reference:

 template <class T>
 class MyContainer
 {
      //..,
      T& operator[](size_t);
      const T& operator[](size_t) const;
 }
不奢求什么 2024-09-02 21:43:24

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
}

The const version will be called on const Matrices.
On non-const matrices the non-const version will be called.

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
}
单身狗的梦 2024-09-02 21:43:24

调用哪个函数取决于对象是否为const。对于 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

这同样适用于指针。如果调用是通过指向 const 的指针完成的,则会调用 const 重载。否则调用非常量重载。

Which function is called depends on whether the object is const. For const objects const overload is called:

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. Otherwise a non-const overload is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文