Const限定符结束函数

const qualifier end of function

本文关键字:结束 函数 Const      更新时间:2023-10-16
#include <iostream>
using namespace std;
class Point {
private:
   int x, y; // Private data members
public:
   Point(int x = 0, int y = 0); // Constructor
   int getX() const; // Getters
   int getY() const;
   void setX(int x); // Setters
   void setY(int y);
   void print() const;
   const Point operator+(const Point & rhs);
         // Overload '+' operator as member function of the class
};
int main(int argc, char** argv)
{
    Point p1(1, 2), p2(4, 5);
   // Use overloaded operator +
   Point p3 = p1 + p2;
   p1.print();  // (1,2)
   p2.print();  // (4,5)
   p3.print();  // (5,7)
   // Invoke via usual dot syntax, same as p1+p2
   Point p4 = p1.operator+(p2);
   p4.print();  // (5,7)
   // Chaining
   Point p5 = p1 + p2 + p3 + p4;
   p5.print();  // (15,21)

    return 0;
}
// Constructor - The default values are specified in the declaration
Point::Point(int x, int y) : x(x), y(y) { } // Using initializer list
// Getters
int Point::getX() const { return x; }
int Point::getY() const { return y; }

// Setters
void Point::setX(int x) { this->x
= x;  }   // (*this).x = x; x = x
void Point::setY(int y) { this->y = y; }
// Public Functions
void Point::print() const {
   cout << "(" << x << "," << y << ")" << endl;
}
// Member function overloading '+' operator
const Point Point::operator+(const Point & rhs) {
   return Point(x + rhs.x, y + rhs.y);
}

我正在学习操作符重载,我不明白为什么会出现错误。

error: no match for 'operator+' (operand types are 'const Point' and 'Point')

我故意删除了operator+函数末尾的const限定符,以便理解它。有人能解释清楚我为什么需要它吗?

成员

const Point Point::operator+(const Point & rhs);

是一个非const成员,即要求操作的lhs是可变的,但是(如错误消息所示)您需要具有const lhs的操作。因此,必须将操作符声明为

Point Point::operator+(const Point & rhs) const;

请注意,我还删除了返回类型的const,因为它已被弃用。


为什么需要const ?自然的+运算符(例如在算术类型之间)不会改变其参数,因此,使用该运算符的通常(人类)约定隐式地假设参数不会改变。在您的特殊情况下,a+b的返回显式地为const(尽管在AFAIK中已弃用),因此在a+b+c = (a+b)+c中lhs是const,并且您的非const成员函数不能使用。

此外,只要成员函数不改变其对象的状态,就应该将其声明为const,以便可以为const对象调用它。


或者,该操作符可以定义为非成员函数friend

Point operator+(const Point&lhs, const Point&rhs);

更清楚地表达了LHS和RHS之间的对称性(非const成员的对应函数应该是

)
Point operator+(Point&lhs, const Point&rhs);

)。

您需要将您的方法定义为const方法,:

const Point operator+(const Point & rhs) const;

p1+p2返回const Point,因此需要const Point + const Point运算符才能计算(p1+p2)+p3

回答得很好,但是他们没有解释为什么 const是一个问题。原因很简单,就是链接顺序和操作符类型匹配。

Point p5 = p1 + p2 + p3 + p4;
// is interpreted as:
Point p5 = ConstPoint( (Point(p1)).operator+((const Point&)(p2)) + p3 + p4;
// as operator+ is left-to-right , not right-to-left !!!

Point和const Point是不同的类型,所以在第一次解释中,有:

p5 = const Point(sumP1P2) . operator+ (Point(P3)) /*+ P4 awaiting interpretation */;
// whose search will be for a const LHS - i.e. "Point operator=(constPointRef) const;" - which is bound to fail.

下面的命令可以工作:

const P& operator+(const P& rhs) const; // corresponds to const P& result = const P& lhs + const P& rhs
P operator+(P rhs) const; // corresponds to copy-by-value for all operands.

唯一的问题是const点运算符+(const点&rhs);在def和dec中删除它。它会工作的