C++ 运算符重载

C++ 运算符重载,c++,operator-overloading,output,C++,Operator Overloading,Output,我对一个关于运算符重载的主题感到困惑。请参阅以下代码: #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 &l

我对一个关于运算符重载的主题感到困惑。请参阅以下代码:

#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;
}
#包括;
类向量{
公众:
int x,y;
向量(){};
向量(int a,int b){
x=a,y=b;
}
向量运算符+(向量aso){
全新的载体;

std::cout您的代码创建新的
向量
对象
brandNew
,然后将初始化为0的值添加到传入的
向量
对象内的值,而不是当前对象中的值。这就是为什么将
v1
添加到
v2
时,结果具有相同的值与
v2
中的相同

替换

brandNew.x = brandNew.x + aso.x;
brandNew.y = brandNew.y + aso.y;


您的代码创建新的
向量
对象
brandNew
,然后将初始化为0的值添加到传入的
向量
对象内的值,而不是当前对象中的值。这就是为什么将
v1
添加到
v2
时,结果与当前对象中的值相同内部
v2

替换

brandNew.x = brandNew.x + aso.x;
brandNew.y = brandNew.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);
    };
向量运算符+(常量向量和aso){
全新的载体;
你的意思可能是

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);
    };
向量运算符+(常量向量和aso){
全新的载体;

std::cout您尚未初始化
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);
};
向量运算符+(向量aso){ 全新的载体;
std::cout您尚未初始化
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);
};
向量运算符+(向量aso){ 全新的载体;
std::cout您还可以使用外部运算符+:

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;
}
类向量{
公众:
int x,y;
向量(){};
向量(int a,int b){
x=a,y=b;
}
友元向量运算符+(常数向量&v1,常数向量&v2);
};
向量运算符+(常量向量和v1,常量向量和v2){
全新的载体;
brandNew.x=v1.x+v2.x;
brandNew.y=v1.y+v2.y;
返回(全新);
};
int main(){
向量v1(2,3);
向量v2(4,5);
向量v3;
v3=v1+v2;

std::cout您还可以使用外部运算符+:

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;
}
类向量{
公众:
int x,y;
向量(){};
向量(int a,int b){
x=a,y=b;
}
友元向量运算符+(常数向量&v1,常数向量&v2);
};
向量运算符+(常量向量和v1,常量向量和v2){
全新的载体;
brandNew.x=v1.x+v2.x;
brandNew.y=v1.y+v2.y;
返回(全新);
};
int main(){
向量v1(2,3);
向量v2(4,5);
向量v3;
v3=v1+v2;

std::cout对+运算符的正确定义如下:

  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);
  }
向量和运算符+(常量向量和aso)
{

std::cout对+运算符的正确定义如下:

  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);
  }
向量和运算符+(常量向量和aso)
{


std::cout Try brandNew.x=x+aso.x…Try brandNew.x=x+aso.x…这是如何工作的?像brandNew.x=x+aso.x;brandNew.y=y+aso.y这样解释;像brandNew.x=this->x+aso.x一样思考它;我在这里很困惑..x是什么?你不想让实例的实际成员值参与进来吗(
x
在这个上下文中相当于
this->x
。@AmanuelBogale看看这里的表,看看带两个参数的运算符重载是如何工作的,但是如何工作的?像brandNew.x=x+aso.x;brandNew.y=y+aso.y这样解释;像brandNew.x=this->x+aso.x一样思考它;我在这里很困惑..什么是x?你不想吗要包含实例的实际成员值(
x
相当于此上下文中的
this->x
。@AmanuelBogale查看此处的表格,了解运算符如何使用2个参数重载works@AmanuelBogale“你认为我应该学习所有这些键盘工作吗?”当然,这些关键字都是必不可少的,只是在初学者级别。@ AAMUUEL博格尔实际上不需要代码>朋友< /C> >,但是如果您将X和Y私有,那么它允许外部操作符+访问X和Y,即使它们是私有的。BTW.您读什么书?C++中的类/重载在560页,没有FRIEN。D关键词…它是一个更好的整体……和LOL我实际上看ViDOOS。IDK如果这是一个好的计划,但我计划完成这个教程…然后完成NeWBSTON C++教程…然后完成另一个100集教程…然后完成3个书LOL。我的一个书待办事项列表显然是C++ + PrimeIe。还是在乞讨!我完全理解你的朋友关键字…大多数时候我会声明我的“财产”是私有的…所以essenc@AmanuelBogale你应该停止尝试从YouTube教程中学习C++。ῥεῖ ... 我的书通常是压倒性的…所以我通常学习它们时,我有一个良好的基础已经…@阿曼纽尔博格尔“你认为我应该学习所有这些键Word LOL?”当然,这些关键字都是必不可少的,只是在初学者级别。@ AAMUUEL博格尔实际上不需要代码>朋友< /C> >,但是如果您将X和Y私有,那么它允许外部操作符+访问X和Y,即使它们是私有的。BTW.您读什么书?C++中的类/重载在560页,没有FRIEN。D关键词…它是一个更好的整体……和LOL我实际上看ViDOOS。IDK如果这是一个好的计划,但我计划完成这个教程…然后完成NeWBSTON C++教程…然后完成另一个100集教程…然后完成3个书LOL。我的一个书待办事项列表显然是C++ + PrimeIe。还是在乞讨!我完全理解你的朋友关键字…大多数时候我会声明我的“财产”是私有的…所以essenc@Aman