Const限定符结束函数
const qualifier end of function
#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中删除它。它会工作的
相关文章:
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 在函数结束后使用指向变量的指针是否安全?
- 如何显示函数开始、结束行和函数体?
- 钩/绕道 d3d9 (现在/结束场景) - 似乎调用我的函数然后崩溃
- 防止我的向量在调用它的函数结束时被删除
- C++未使用的变量警告,即使我在函数结束时返回它
- 编写一个函数,在开始和结束处添加括号,使所有括号匹配并返回
- 函数结束后指针会死亡吗?
- 函数结束后覆盖的指针
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 如何在动画结束后调用函数
- 如何在函数结束时有效地释放矢量内存
- 如何结束图形GUI和执行控制台函数(QT)
- 将数组作为函数参数传递,并在其上调用开始/结束方法
- 在 C++ 中结束函数
- 无法使用非成员开始和结束函数写入基于范围
- Const限定符结束函数
- 结束函数,同时从该点继续读取文件
- 为什么在c++ 11中使用非成员的开始和结束函数
- 开始和结束函数用于内置数组类型