运算符重载
Operator Overloading
我对有关运算符重载的话题感到困惑。见以下代码:
#include <iostream>;
class Vectors {
public:
int x, y;
Vectors() {};
Vectors(int a,int b) {
x = a, y = b;
}
Vectors operator+(Vectors aso) {
Vectors brandNew;
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
brandNew.x = brandNew.x + aso.x;
brandNew.y = brandNew.y + aso.y;
return (brandNew);
};
};
int main() {
Vectors v1(2,3);
Vectors v2(4,5);
Vectors v3;
v3 = v1 + v2;
std::cout << "Vector V3 X : " << v3.x << std::endl;
std::cout << "VECTOR V3 Y : " << v3.y << std::endl;
}
当我打印 aso.x 时,y 给了我 4 和 5。我想做的是同时添加 v1 和 v2;表示 v1 和 v2 的 x,以及 v1 和 v2 的 y。然后,将其传递给 Vectors 对象和 return 该对象。
根据我的上述情况,我该如何实现?
您的代码创建了新的 Vectors
对象 brandNew
,然后将其初始化为 0 的值添加到您传入的 Vectors
对象中的值,而不是当前对象中的值。这就是为什么将 v1
添加到 v2
结果与 v2
.
中的值相同的原因
替换
brandNew.x = brandNew.x + aso.x;
brandNew.y = brandNew.y + aso.y;
和
brandNew.x = x + aso.x;
brandNew.y = y + aso.y;
你的意思可能是
Vectors operator+(const Vectors& aso) {
Vectors brandNew;
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
brandNew.x = x + aso.x;
brandNew.y = y + aso.y;
return (brandNew);
};
或
Vectors operator+(const Vectors& aso) {
Vectors brandNew(x + aso.x, y + aso.y);
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
return (brandNew);
};
根据您的评论
But about if there were three.
像这样链接运算符
int main() {
Vectors v1(2,3);
Vectors v2(4,5);
Vectors v3(7,11);
Vectors v4;
v4 = v1 + v2 + v3;
std::cout << "Vector V4 X : " << v4.x << std::endl;
std::cout << "Vector V4 X : " << v4.y << std::endl;
}
您尚未初始化 brandNew
的 x
和 y
成员。您会得到随机结果。
Vectors operator+(Vectors aso) {
Vectors brandNew;
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
brandNew.x = x + aso.x;
brandNew.y = y + aso.y;
return (brandNew);
};
你也可以使用外部运算符+ :
class Vectors {
public:
int x, y;
Vectors() {};
Vectors(int a,int b) {
x = a, y = b;
}
friend Vectors operator+(const Vectors& v1, const Vectors& v2);
};
Vectors operator+(const Vectors& v1, const Vectors& v2) {
Vectors brandNew;
brandNew.x = v1.x + v2.x;
brandNew.y = v1.y + v2.y;
return (brandNew);
};
int main() {
Vectors v1(2,3);
Vectors v2(4,5);
Vectors v3;
v3 = v1 + v2;
std::cout << "Vector V3 X : " << v3.x << std::endl;
std::cout << "VECTOR V3 Y : " << v3.y << std::endl;
}
+运算符的正确定义如下:
Vectors &operator+(const Vectors& aso)
{
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
x = x + aso.x;
y = y + aso.y;
return (*this);
}
以上代码不需要临时变量,也不需要像通过引用传递的那样对参数进行不必要的复制。
我对有关运算符重载的话题感到困惑。见以下代码:
#include <iostream>;
class Vectors {
public:
int x, y;
Vectors() {};
Vectors(int a,int b) {
x = a, y = b;
}
Vectors operator+(Vectors aso) {
Vectors brandNew;
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
brandNew.x = brandNew.x + aso.x;
brandNew.y = brandNew.y + aso.y;
return (brandNew);
};
};
int main() {
Vectors v1(2,3);
Vectors v2(4,5);
Vectors v3;
v3 = v1 + v2;
std::cout << "Vector V3 X : " << v3.x << std::endl;
std::cout << "VECTOR V3 Y : " << v3.y << std::endl;
}
当我打印 aso.x 时,y 给了我 4 和 5。我想做的是同时添加 v1 和 v2;表示 v1 和 v2 的 x,以及 v1 和 v2 的 y。然后,将其传递给 Vectors 对象和 return 该对象。
根据我的上述情况,我该如何实现?
您的代码创建了新的 Vectors
对象 brandNew
,然后将其初始化为 0 的值添加到您传入的 Vectors
对象中的值,而不是当前对象中的值。这就是为什么将 v1
添加到 v2
结果与 v2
.
替换
brandNew.x = brandNew.x + aso.x;
brandNew.y = brandNew.y + aso.y;
和
brandNew.x = x + aso.x;
brandNew.y = y + aso.y;
你的意思可能是
Vectors operator+(const Vectors& aso) {
Vectors brandNew;
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
brandNew.x = x + aso.x;
brandNew.y = y + aso.y;
return (brandNew);
};
或
Vectors operator+(const Vectors& aso) {
Vectors brandNew(x + aso.x, y + aso.y);
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
return (brandNew);
};
根据您的评论
But about if there were three.
像这样链接运算符
int main() {
Vectors v1(2,3);
Vectors v2(4,5);
Vectors v3(7,11);
Vectors v4;
v4 = v1 + v2 + v3;
std::cout << "Vector V4 X : " << v4.x << std::endl;
std::cout << "Vector V4 X : " << v4.y << std::endl;
}
您尚未初始化 brandNew
的 x
和 y
成员。您会得到随机结果。
Vectors operator+(Vectors aso) {
Vectors brandNew;
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
brandNew.x = x + aso.x;
brandNew.y = y + aso.y;
return (brandNew);
};
你也可以使用外部运算符+ :
class Vectors {
public:
int x, y;
Vectors() {};
Vectors(int a,int b) {
x = a, y = b;
}
friend Vectors operator+(const Vectors& v1, const Vectors& v2);
};
Vectors operator+(const Vectors& v1, const Vectors& v2) {
Vectors brandNew;
brandNew.x = v1.x + v2.x;
brandNew.y = v1.y + v2.y;
return (brandNew);
};
int main() {
Vectors v1(2,3);
Vectors v2(4,5);
Vectors v3;
v3 = v1 + v2;
std::cout << "Vector V3 X : " << v3.x << std::endl;
std::cout << "VECTOR V3 Y : " << v3.y << std::endl;
}
+运算符的正确定义如下:
Vectors &operator+(const Vectors& aso)
{
std::cout << "ASO X " << aso.x << std::endl;
std::cout << "ASO Y " << aso.y << std::endl;
x = x + aso.x;
y = y + aso.y;
return (*this);
}
以上代码不需要临时变量,也不需要像通过引用传递的那样对参数进行不必要的复制。