【问题标题】:Overloading + and += operators in parent and child class in C++在 C++ 中重载父类和子类中的 + 和 += 运算符
【发布时间】:2021-07-09 13:12:21
【问题描述】:

好吧,伙计们,这让我很困惑,尤其是因为网上的信息太少了......

好吧,假设我有一个父类和一个子类:

   class ParentPlot
   {
      public:
         int a; 
         int b;
         datacontainer Events;


         friend ParentPlot& operator+(ParentPlot& plotA, const ParentPlot& plotB)
         {
            //add stuff
            return sumPlot;
         }
         virtual ParentPlot operator+=(const ParentPlot &plotB)
         {
            //+= stuff using 'this'
            return *this;
         }
   }

   class ChildPlot: public ParentPlot
   {
      public:
         int q;

      friend ChildPlot& operator+(ChildPlot& plotA, const ChildPlot& plotB)
         {
             //Static cast and add the parents.
            ParentPlot PlotACast = static_cast<ParentPlot>(plotA); //2 Static casts
            ParentPlot PlotBCast = static_cast<ParentPlot>(plotB);
            ParentPlot ParentSum = PlotACast + PlotBCast; //Add as ParentPlots
            ChildPlot BasePlot = ChildPlot(ParentSum);

            //Add child members
            BasePlot.q = plotA.q + plotB.q;

            return BasePlot;
         }
      ChildPlot& operator+=(const ChildPlot& plotB)
      {
          //How do I correctly implement this?
      }
   }

我应该如何正确管理这些类的添加和 +=?每个组合似乎都会产生某种错误,我无法让它发挥作用。例如上面实现的方法给出了以下错误:

"这个算子函数的参数太少"

我曾尝试将 ParentPlot 设为抽象类,然后仅在子类中实现,但随后加法运算符中断,因为我无法在加法运算符中返回父级。

从技术上讲,ParentPlot 将是抽象的,但会有许多需要求和的子节点,并且它有多个成员。我不应该为父母和孩子实现加法运算符或 += 还是我错过了什么? Genuinly 不知道处理这个问题的正确方法。任何想法都会有所帮助...

谢谢

【问题讨论】:

  • + 无法返回引用,因为它需要创建一个新对象。 (虚拟运算符通常不像您最初想象的那么好。)
  • 不应该 += 不返回作为参考的“this”吗?
  • this 是指针,而不是引用。 += 预计将通过引用返回 *this,修改后的对象(您可以通过不返回任何内容来解决切片问题)。另一方面,您的 + 正在返回对 BasePlot 的引用,这是一个自动对象,并在函数返回时被销毁。
  • 作为一般的经验法则,变异操作符——比如+=——返回一个对修改对象的引用,*this。非变异运算符 - 如+ - 按值返回一个新对象。

标签: c++ inheritance operator-overloading operators abstract-class


【解决方案1】:

1。修复您的运算符返回类型。

operator + 应该返回一个副本。 operator += 应该返回对 this 的引用。

friend ParentPlot operator+(ParentPlot& plotA, const ParentPlot& plotB)
ParentPlot& operator+=(const ParentPlot &plotB)
friend ChildPlot operator+(ChildPlot& plotA, const ChildPlot& plotB)
ChildPlot& operator+=(const ChildPlot& plotB)

2。使用operator += 实现您的operator +

这个小技巧可以让您的代码干燥,并且可以更轻松地实现这些额外的运算符。
注意第一个参数是取值的,我们需要一个由operator +=修改的副本。

friend ParentPlot operator+(ParentPlot plotA, const ParentPlot& plotB)
{
    return plotA += plotB;
}

friend ChildPlot operator+(ChildPlot plotA, const ChildPlot& plotB)
{
    return plotA += plotB;
}

3。实现子operator +=

假设您已经拥有operator += 对应ParentPlot,您可以通过两种方式使用它:

static_cast<ParentPlot&>(*this) += plotB;
//or
ParentPlot::operator+=(plotB);

然后像往常一样添加特定于子的实现和return *this;

完整代码(see it online):

class ParentPlot {
public:
    int a;
    int b;

    friend ParentPlot operator+(ParentPlot plotA, const ParentPlot& plotB)
    {
        return plotA += plotB;
    }
    ParentPlot& operator+=(const ParentPlot& plotB)
    {
        a += plotB.a;
        b += plotB.b;
        return *this;
    }
};

class ChildPlot : public ParentPlot {
public:
    int q;

    friend ChildPlot operator+(ChildPlot plotA, const ChildPlot& plotB)
    {
        return plotA += plotB;
    }
    ChildPlot& operator+=(const ChildPlot& plotB)
    {
        //static_cast<ParentPlot&>(*this) += plotB;
        ParentPlot::operator+=(plotB);
        q += plotB.q;
        return *this;
    }
};

【讨论】:

    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2021-06-07
    • 2021-05-14
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    相关资源
    最近更新 更多