C++ 如何在c+中重载赋值运算符+;?

C++ 如何在c+中重载赋值运算符+;?,c++,operator-overloading,assignment-operator,C++,Operator Overloading,Assignment Operator,我有以下结构: struct mystruct{ int a; int b; int c; } 我只想重载“=”使mystruct A=mystruct B 等于: A.a = B.a; A.b = B.b; A.c = B.c; (分别为字段赋值) 我该怎么做呢?自动生成的赋值操作符已经像那样工作了。但假设这只是一个示例,您还想做其他事情,请考虑: struct mystruct{ int a; int b; int c; myst

我有以下结构:

struct mystruct{
    int a;
    int b;
    int c;
}
我只想重载“=”使
mystruct A=mystruct B
等于:

A.a = B.a;
A.b = B.b;
A.c = B.c;
(分别为字段赋值)


我该怎么做呢?

自动生成的赋值操作符已经像那样工作了。但假设这只是一个示例,您还想做其他事情,请考虑:

struct mystruct{
    int a;
    int b;
    int c;

    mystruct& operator=(const mystruct& other)
    {
        a = other.a;
        b = other.b;
        c = other.c;
        return *this;
    }
}
struct mystruct {
  int a;
  int b;
  int c;
  mystruct& operator=(const mystruct& other) {
    this->a = other.a;
    this->b = other.b;
    this->c = other.c;
    return *this;
  }
};

自动生成的赋值运算符已按此方式工作。但假设这只是一个示例,您还想做其他事情,请考虑:

struct mystruct {
  int a;
  int b;
  int c;
  mystruct& operator=(const mystruct& other) {
    this->a = other.a;
    this->b = other.b;
    this->c = other.c;
    return *this;
  }
};

请澄清您使用的编程语言。@Raymond当我使用CPP时,默认的赋值运算符按您的要求工作。i、 e.您不需要重载
=
运算符。你有什么问题?你不知道怎么做操作符重载?你检查了C++的任何一本介绍书吗?请说明你使用的是什么编程语言。@ RaymondChen。我使用的是CPPUT默认赋值运算符。i、 e.您不需要重载
=
运算符。你有什么问题?你不知道怎么做操作符重载?你检查过C++的介绍书吗?