使创建的对象再次彼此相等

时间:2018-06-29 09:06:01

标签: c++ object equals

我最初有两个对象(a和b),并且b = a。有些操作是在a上执行的,我想知道如何再次使b = a。

例如,

int main()
{
  A a;
  a.some_ops();
  /// I to create a new object and make it equal to a
  A b = a;
  b.some_ops();

  a.other_ops();
  /// Now I want to make b = a again 
  b = a; // BUG, this will not work, right?
}

谢谢您的时间。

1 个答案:

答案 0 :(得分:0)

C ++为每个class生成一个默认赋值运算符,该赋值运算符按成员分配实例成员(使用成员类/类型的赋值运算符)。

如果这不是故意的,则可以删除赋值运算符(然后赋值被拒绝)或重载。

我稍微丰富了OP的示例代码:

#include <iostream>

class A {
  private:
    int _mem1, _mem2;
  public:
    A(): _mem1(0), _mem2(0) { }
    A(const A&) = default;
    A& operator=(const A&) = default;
    void some_ops() { ++_mem1; }
    void other_ops() { ++_mem2; }

    friend std::ostream& operator << (std::ostream &out, const A &a)
    {
      return out << "mem1: " << a._mem1 << ", mem2: " << a._mem2;
    }
};

int main()
{
  std::cout << "A a;\n"; A a;
  std::cout << "a: " << a << '\n';
  std::cout << "a.some_ops;\n"; a.some_ops();
  std::cout << "a: " << a << '\n';
  /// I to create a new object and make it equal to a
  std::cout << "A b = a;\n"; A b = a;
  std::cout << "a: " << a << ", b: " << b << '\n';
  std::cout << "b.some_ops();\n"; b.some_ops();
  std::cout << "a: " << a << ", b: " << b << '\n';

  std::cout << "a.other_ops();\n"; a.other_ops();
  std::cout << "a: " << a << ", b: " << b << '\n';
  /// Now I want to make b = a again 
  std::cout << "b = a;\n"; b = a; // BUG, this will not work, right? <= It should.
  std::cout << "a: " << a << ", b: " << b << '\n';
  return 0;
}

输出:

A a;
a: mem1: 0, mem2: 0
a.some_ops;
a: mem1: 1, mem2: 0
A b = a;
a: mem1: 1, mem2: 0, b: mem1: 1, mem2: 0
b.some_ops();
a: mem1: 1, mem2: 0, b: mem1: 2, mem2: 0
a.other_ops();
a: mem1: 1, mem2: 1, b: mem1: 2, mem2: 0
b = a;
a: mem1: 1, mem2: 1, b: mem1: 1, mem2: 1

Live Demo on coliru


第一个示例代码–具有重载的复制构造函数和赋值运算符:

#include <iostream>

class A {
  private:
    int _mem1, _mem2;
  public:
    A(): _mem1(0), _mem2(0) { }
    A(const A &a):
      _mem1(a._mem1), _mem2(a._mem2)
    {
      std::cout << "A::A(const A&) called\n";
    }
    A& operator=(const A &a)
    {
      _mem1 = a._mem1; _mem2 = a._mem2;
      std::cout << "A& A::operate=(const A&) called\n";
      return *this;
    }
    void some_ops() { ++_mem1; }
    void other_ops() { ++_mem2; }

    friend std::ostream& operator << (std::ostream &out, const A &a)
    {
      return out << "mem1: " << a._mem1 << ", mem2: " << a._mem2;
    }
};

int main()
{
  std::cout << "A a;\n"; A a;
  std::cout << "a: " << a << '\n';
  std::cout << "a.some_ops;\n"; a.some_ops();
  std::cout << "a: " << a << '\n';
  /// I to create a new object and make it equal to a
  std::cout << "A b = a;\n"; A b = a;
  std::cout << "a: " << a << ", b: " << b << '\n';
  std::cout << "b.some_ops();\n"; b.some_ops();
  std::cout << "a: " << a << ", b: " << b << '\n';

  std::cout << "a.other_ops();\n"; a.other_ops();
  std::cout << "a: " << a << ", b: " << b << '\n';
  /// Now I want to make b = a again 
  std::cout << "b = a;\n"; b = a; // BUG, this will not work, right? <= It should.
  std::cout << "a: " << a << ", b: " << b << '\n';
  return 0;
}

输出:

A a;
a: mem1: 0, mem2: 0
a.some_ops;
a: mem1: 1, mem2: 0
A b = a;
A::A(const A&) called
a: mem1: 1, mem2: 0, b: mem1: 1, mem2: 0
b.some_ops();
a: mem1: 1, mem2: 0, b: mem1: 2, mem2: 0
a.other_ops();
a: mem1: 1, mem2: 1, b: mem1: 2, mem2: 0
b = a;
A& A::operate=(const A&) called
a: mem1: 1, mem2: 1, b: mem1: 1, mem2: 1

Live Demo on coliru