【问题标题】:Creating an assignment (=) operator for class in C++ [duplicate]在 C++ 中为类创建赋值(=)运算符 [重复]
【发布时间】:2011-10-07 17:22:09
【问题描述】:

可能重复:
Operator overloading

编辑 2

我错误地使用了 insert(...),我实际上并不需要 '=' 运算符。很抱歉浪费人们的时间。我已经投票结束.. 还剩 2 票。请投票。

编辑

我想要一个 '=' 运算符的原因是我可以在 Derivation 对象的向量上使用 insert(...) 函数。目前我的编译器说:

/usr/include/c++/4.2.1/bits/stl_algobase.h:283: error: no match for 'operator=' in '* __result = * __first'

我之前为自己的类创建了 '==' 和 '

class Derivation {
public:
    string                  rc; 
    ImplementationChoice    Y; 
    vector<Derivation>      X;
    vector<string>          D;       
    vector<string>          C;       
    vector<Player>          P, O;   
    vector<Attack>          B;   

    // various functions
    // ...
};

我想知道我需要放入什么

// What do '=' return?  An object of the class right?
Derivation& operator=(const Derivation &d) const {
    // something....
}

非常感谢。

【问题讨论】:

  • 至少,它应该返回一个引用 (Derivation&amp;) 而不是对象的新副本。 :)
  • 谢谢您.. 现在编辑帖子。抱歉.. 对 C++ 很陌生
  • 我建议你阅读operator overloading faq
  • 如果 ImplementationChoice 已经提供了一个operator=,则很可能已经隐式声明了一个适当的Derivation::operator=。 (没有给出答案,因为它并没有真正解决你的问题,只是把它作为一个提醒。)

标签: c++


【解决方案1】:

这取决于你,真的。你需要运营商做什么?您要返回参考资料,还是要一份副本?

编辑:请注意,这是修辞。您使用此向量的目的将决定您是否需要参考或副本。例如,如果您插入的对象在从向量中删除之前随时超出范围,您将需要一个副本。如果没有,并且您希望在更改向量中的实例时影响原始对象,您将需要一个引用。希望对您有所帮助。

【讨论】:

  • 请在帖子中编辑。我需要 '=' 运算符,这样我就可以在 Derivation 对象的向量上使用 insert(...)
  • 我希望能够做到这一点:vector&lt;Derivation&gt; d;d.insert(d.end(), vec.begin(), vec.end());
  • 从你编辑看我想做什么,我相信这是我需要的参考。谢谢。
【解决方案2】:

实现赋值运算符的标准方法是复制和交换。 这样做的优点是最简单的方法可以使赋值运算符在面对异常和自赋值时是正确的。它还根据复制构造函数定义了赋值操作,从而减少了在向类中添加额外成员时需要更改代码的位置。

无论如何 - 这就是你的情况:

class Derivation {
    public:
    string                  rc; 
    ImplementationChoice    Y; 
    vector<Derivation>      X;
    vector<string>          D;       
    vector<string>          C;       
    vector<Player>          P, O;   
    vector<Attack>          B;   

    //You need to add a swap function to your class
    void swap(Derivation& o) {
        rc.swap(o.rc);
        Y.swap(o.Y);//Assuming ImplementationChoice has a swap function (it should!)
        X.swap(o.X);
        D.swap(o.D);
        C.swap(o.C);
        P.swap(o.P);
        O.swap(o.O);
        B.swap(o.B);
    }
    Derivation& operator=(Derivation const& o) {
        Derivation copy(o);
        copy.swap(*this);
        return *this;
    }
    // various functions
    // ...
};

【讨论】:

  • 谢谢曼卡斯。您在我的帖子中看到我的编辑了吗?如果我想在我的 Derivation 对象的向量上使用向量 insert(...) 函数,这是我需要的吗?谢谢。
  • @alemaster - 不,插入函数将使用复制构造函数将 Derivation 对象插入向量中。编译器生成的复制构造函数应该适合这个类。
【解决方案3】:

因为 - @jalf 没有给出答案,所以这里是:)

Derivation& operator=(const Derivation &d) {
  // something....
  return *this;
}

您需要返回对此实例的引用。 this 是一个关键字,它包含一个指向操作员所作用的实例的指针。

【讨论】:

    【解决方案4】:

    首先删除 const ... 那么如果你真的需要一个复制操作符,做类似的事情并添加你自己的逻辑(这样它就不会完全按照编译器生成的复制操作符来做):

    Derivation& operator=(const Derivation& other) {
        this->rc = other.rc; 
        this->Y = other.Y; 
        this->X = other.X;
        this->D = other.D;       
        this->C = other.C;       
        this->P = other.P;
        this->O = other.O;   
        this->B = other.B;
        // ...
        return *this;
    }  
    

    【讨论】:

    • 除了这正是编译器生成的复制赋值运算符所做的......
    • @Armen - 真的吗?我没有意识到编译器生成的赋值运算符也为vector 成员调用了赋值?
    【解决方案5】:

    你不需要一个。编译器生成的就可以了。

    【讨论】:

    • 这取决于如何实现 ImplementationChoice、Player 和 Attack...
    • 真的吗?这个struct中有五个vector...
    • 请在帖子中编辑。我需要 '=' 运算符,这样我就可以在 Derivation 对象的向量上使用 insert(...) 。没有它就无法编译。它说/usr/include/c++/4.2.1/bits/stl_algobase.h:283: error: no match for 'operator=' in '* __result = * __first'
    【解决方案6】:

    首先,赋值运算符可能不应该是 const--

    其次,赋值运算符通常返回一个非常量引用,该引用指向被赋值的对象 (*this)

    【讨论】:

      【解决方案7】:

      要重载赋值运算符,你应该这样做

      Derivation& operator=(const Derivation &d)  {
          // something....
         return *this
      }
      

      这将允许你做类似的事情。

      偏差a,b,c; //某事

      c = b = a;

      【讨论】:

      • 我不认为它应该是一个 const 方法,因为您正在更改对象的状态(为其分配 d 的值)。它可能是合法的 C++,但实际上没有意义。
      • @Martinho 和@Skizz。谢谢,我已经编辑了
      • 谢谢.. 我基本上从代码中其他地方的“
      • @alemaster 删除什么。我不太明白
      • @Leon - 忽略我!我刚刚意识到 '=' 必须只有一个参数。我尝试删除 const Derivation &amp;d 但编译器抱怨。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 2019-11-17
      • 2020-04-17
      • 2015-12-30
      • 2017-11-21
      • 2012-04-26
      相关资源
      最近更新 更多