C++:在派生类中重用运算符+()

2023-12-30

有没有办法重复使用operator+(...)基类的方法与赋值运算符类似operator=(...)下面的例子中使用的是?

重要提示:我不想使用抽象类/虚拟方法,我只是对下面的示例感到好奇,以便我充分理解继承的“基本”用法,而不必过多地了解多态性!下面的例子中用operator=()可以成功,用operator+()也可以吗?

基类:

class Vector
{
private:
  int _x;
  int _y;
protected:

  // constructors & destructors here

  Vector& operator=(const Vector& source){...}

  Vector operator+(const Vector& source) const{...}

};

派生类:

class Position : public Vector
{
private:
  double _rho;
  double _phi;
public:

  // constructors & destructor here

  Position& operator=(const Position& source)
  {
    Vector::operator=(source); // slices source to its base-class part
    _rho = source._rho;
    _phi = source._phi;
    return *this;
  }

  Position operator+(const Position& source) const
  {
    /* 
    * HELP NEEDED HERE:
    * Some way of calling Vector::operator+() to avoid
    * code repetition and ensure consistency with the 
    * operator+ definition in the base class
    */
  }
};

谢谢


我最初的评论虽然有点轻率,但仍然正确。话虽如此,我将您的问题重新实现为可以编译以演示解决方案的东西。

首先,您的类(如图所示)不需要赋值运算符重载。它也不需要复制构造函数、析构函数、移动构造函数或移动赋值运算符。这是因为您没有保持任何动态,因此内置的浅拷贝/分配就足够了。

话虽如此,我重新实现了你的operator+()以稍微更惯用的方式。如果你能做到Derived + Derived,人们期望能够做到Derived += Derived以及。

因为这些是相互关联的,所以可以实现operator+() 按照 operator+=()。唯一的技巧是利用值传递。

这是代码:

#include <iostream>

class Base {
 public:
  Base() = default;
  Base(int x, int y) : m_x(x), m_y(y) {}

  // Assignment operator not required, Rule of 0

  Base& operator+=(const Base& rhs) {
    m_x += rhs.m_x;
    m_y += rhs.m_y;

    return *this;
  }

  friend Base operator+(Base lhs, const Base& rhs) {
    lhs += rhs;

    return lhs;
  }

 // Needed to print a Derived object
 protected:
  int getX() const { return m_x; }
  int getY() const { return m_y; }

 private:
  int m_x = 0;
  int m_y = 0;
};

class Derived : public Base {
 public:
  Derived() = default;
  Derived(double rho, double phi) : Base(), m_rho(rho), m_phi(phi) {}
  Derived(int x, int y, double rho, double phi)
      : Base(x, y), m_rho(rho), m_phi(phi) {}

  Derived& operator+=(const Derived& other) {
    Base::operator+=(other);
    m_rho += other.m_rho;
    m_phi += other.m_phi;

    return *this;
  }

  friend Derived operator+(Derived lhs, const Derived& rhs) {
    lhs += rhs;

    return lhs;
  }

  friend std::ostream& operator<<(std::ostream& sout, const Derived& rhs) {
    return sout << "x: " << rhs.getX() << "\ny: " << rhs.getY()
                << "\nrho: " << rhs.m_rho << "\nphi: " << rhs.m_phi;
  }

 private:
  double m_rho = 0.0;
  double m_phi = 0.0;
};

int main() {
  Derived d1;
  Derived d2(2, 2, 1.1, 1.1);

  Derived d3 = d1 + d2;
  Derived d4 = d2 + d3;
  std::cout << "\nd4:\n" << d4 << '\n';

  std::cout << "\nd3:\n" << d3 << '\n';

  std::cout << "\nd2:\n" << d2 << '\n';
}

Output:

d4:
x: 4
y: 4
rho: 2.2
phi: 2.2

d3:
x: 2
y: 2
rho: 1.1
phi: 1.1

d2:
x: 2
y: 2
rho: 1.1
phi: 1.1

你可以看到我做了“同样的事情”。在我的operator+=(),我称之为Base版本第一。然后我照顾Derived成员。这是有效的,因为虽然operator+=()返回一个引用,operator+()采取其lhs按值传递参数,这意味着它是一个副本。副本被更改为operator+=()然后回来了。利用按值传递的优势让我可以使用operator+=()不改变任何一个原始操作数。所以operator+()仍然表现如预期。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++:在派生类中重用运算符+() 的相关文章

随机推荐