【发布时间】:2018-03-23 16:16:25
【问题描述】:
我正在创建一个 2D 坐标类(名为“Point”)来帮助我学习 C++。我希望能够对 Point 类对象(例如 Point_a + Point_b)执行基本的算术运算(+、-、*、/ ...)。但是,我也希望能够在 Points 和其他变量类型(int/float/double)之间执行此类操作。
这可以使用运算符/函数重载来完成。从下面的代码中可以看出(仅添加),据我所知,我必须为每个附加变量类型包含两个附加函数,一个用于“Point + int/float/double”形式,一个用于“int/float/double + Point”形式。
#include <iostream>
using namespace std;
class Point
{
private:
double x, y;
public:
Point(double x_in, double y_in){
setX(x_in);
setY(y_in);
}
// Getters and Setters omitted for brevity
// --- Start of Addition Operator Overloads --- (Code Block A)
friend Point operator+(const Point &p1, const Point &p2); //Point + Point
friend Point operator+(const Point &p1, int val); //Point + Int
friend Point operator+(int val, const Point &p1); //Int + Point
friend Point operator+(const Point &p1, float val); //Point + Float
friend Point operator+(float val, const Point &p1); //Float + Point
friend Point operator+(const Point &p1, double val); //Point + Double
friend Point operator+(double val, const Point &p1); //Double + Point
// --- End of Addition Operator Overloads --- (Code Block A)
};
// --- Start of Addition Operator Overload Functions --- (Code Block B)
Point operator+(const Point &p1, const Point &p2){
return Point(p1.x + p2.x, p1.y + p2.y);
}
Point operator+(const Point &p1, int val){
return Point(p1.x + val, p1.y + val);
}
Point operator+(int val, const Point &p1){
return Point(p1.x + val, p1.y + val);
}
Point operator+(const Point &p1, float val){
return Point(p1.x + val, p1.y + val);
}
Point operator+(float val, const Point &p1){
return Point(p1.x + val, p1.y + val);
}
Point operator+(const Point &p1, double val){
return Point(p1.x + val, p1.y + val);
}
Point operator+(double val, const Point &p1){
return Point(p1.x + val, p1.y + val);
}
// --- End of Addition Operator Overload Functions --- (Code Block B)
int main()
{
Point point_a( 2.00, 20.00);
Point point_b( 0.50, 5.00);
Point point_c = point_a + point_b;
cout << "X = " << point_c.getX() << " and Y = " << point_c.getY() << endl;
return 0;
}
似乎有很多重复,特别是在“Point + int/float/double”类型的函数之间。我想知道是否有办法可以将其缩短一点。比如说,我可以有一个版本来处理所有三个,而不是为整数、浮点和双精度提供单独的版本。例如将代码块“A”和“B”转换成类似的东西:
...
// --- Start of Addition Operator Overloads --- (Code Block A)
friend Point operator+(const Point &p1, const Point &p2); //Point + Point
friend Point operator+(const Point &p1, int||float||double val); //Point + Int/Float/Double
friend Point operator+(int||float||double val, const Point &p1); //Int/Float/Double + Point
// --- End of Addition Operator Overloads --- (Code Block A)
...
...
// --- Start of Addition Operator Overload Functions --- (Code Block B)
Point operator+(const Point &p1, const Point &p2){
return Point(p1.x + p2.x, p1.y + p2.y);
}
Point operator+(const Point &p1, int||float||double val){
return Point(p1.x + val, p1.y + val);
}
Point operator+(int||float||double val, const Point &p1){
return Point(p1.x + val, p1.y + val);
}
// --- End of Addition Operator Overload Functions --- (Code Block B)
...
上述代码部分旨在表示所需结果,而不是实际功能(例如 int OR float OR double)。
简而言之,有什么方法可以让“friend Point operator+(const Point &p1, int val);”及其对应的函数(在代码块 B 中)接受整数、浮点数和双精度值,或者我需要为每个变量类型设置一个单独的值吗? ?
感谢您的宝贵时间。
【问题讨论】:
-
可能是一个模板?
-
顺便说一句:Point(double x_in, double y_in){ setX(x_in);设置Y(y_in); } ' 是多余的。您可以直接在构造函数中设置 x 和 y。
标签: c++ class math operator-overloading