在C++重载运算符时"const"是什么意思

what does "const" mean when overloading operator in C++

本文关键字:const 意思 是什么 C++ 重载 运算符      更新时间:2023-10-16

我看到了这样的内容:

const char& operator[] (int Index) const

我理解的第一个const。这是为了保护返回的char不被修改。

但是第二个const是什么意思?为什么我们有时用两个const,有时只用一个?

可用于任何成员函数,而不仅仅是操作符。这意味着,这个函数将:

  • 不能修改任何数据成员(mutable除外)
  • 将无法调用任何非const成员函数