'<<'与继承和多态性超载?

overloading '<<' with inheritance and polymorphism?

本文关键字:lt 超载 多态性 继承      更新时间:2023-10-16

以下是代码外观的粗略示例,问题是我如何让 DerivedOne 和 DerivedTwo 具有重载的 <<运算符,但将这些对象存储在 Base* 的向量中。

至于我想实现的目标;我希望能够遍历对象向量并输出我在 DerivedOne 和 DerivedTwo 中告诉它的信息。

vector<Base*> objects;
class Base
{
 private:
 object Data
 public:
 object getData() { return Data; }
};
class DerivedOne : public Base
{
}
class DerivedTwo : public Base
{
}

现在我知道有这个,但它不适合我的目的。

friend ostream &operator<<(ostream &stream, Object object)
{
    return stream << "Test" << endl;
}

将虚拟方法设为私有,以区分对象的使用方式与派生类自定义对象行为的方式。

这是一个类似于其他答案的解决方案,但虚拟方法是私有的:

#include <iostream>
namespace {
  class Base {
    // private (there is no need to call it in subclasses)
    virtual std::ostream& doprint(std::ostream&) const = 0;
  public:
    friend std::ostream& operator << (std::ostream& os, const Base& b) {
      return b.doprint(os); // polymorphic print via reference
    }
    virtual ~Base() {} // allow polymorphic delete
  };

  class DerivedOne : public Base {
    std::ostream& doprint(std::ostream& os) const {
      return os << "One";
    }
  public:
    DerivedOne() { std::cerr << "hi " << *this << "n"; } // use << inside class
    ~DerivedOne() { std::cerr << "bye " << *this << "n"; }
  };
}

#include <memory>
#include <vector>
int main () {
  using namespace std;
  // wrap Base* with shared_ptr<> to put it in a vector
  vector<shared_ptr<Base>> v{ make_shared<DerivedOne>() };
  for (auto i: v) cout << *i << " ";
  cout << endl;
}

输出

hi One
One 
bye One

你可以做这样的事情:

struct Base {
    virtual ~Base () {}
    virtual std::ostream & output (std::ostream &) const = 0;
};
std::ostream & operator << (std::ostream &os, const Base &b) {
    return b.output(os);
}

然后,当Base上应用 operator<< 时,它将调用派生的输出方法。

就您的问题而言,您有一个动态分配的多态对象的集合。

解决方案可能会根据这些对象遵守某些设计规则的方式而更改:

  1. 您是反对"仅叶子对象"吗?(不是指其他对象本身)?
  2. 是否只有单个非循环引用(您的对象是否仅形成子对象的树)?
  3. 引用之间是否有多条路径(由两个或多个引用的同一对象)
  4. 有循环引用吗?
  5. 是否有对其他对象成员的引用?

情况可能会变得非常复杂,具体取决于情况,取决于您是否可以容忍同一对象的多个输出以及如何打破最终循环。

你不能做的第一件事是使用 Base 作为值参数(就像在你的 中一样,因为这会创建一个副本(承认你的对象是可复制的:如果它们引用其他对象,副本有什么作用? 乘以路径?也克隆引用的对象?

如果你遇到这种情况(1.),你只需要一个虚函数std::ostream&,在所有叶对象中被覆盖。 然后,您需要一个重载用于operator<<(std::ostream&, Base*),另一个重载用于(std::ostream&, const std::vector<Base*>&)

喜欢这个:

class Base
{
    ...
public:
    virtual ~Base() {} //must be polymorphic
protected:
    virtual void out(std::ostream& s) =0;
    friend std::ostream& operator<<(std::ostream& st, Base* pb)
    { pb->out(st); return st; }
};
class Derived1: public Base
{
    ...
protected:
    virtual void out(std::ostream& s)
    {
        s << "Derived1 at " << this << " address " << std::endl;
    }
};

class Derived2: public Base
{
    ...
protected:
    virtual void out(std::ostream& s)
    {
        s << "Derived2 at " << this << " address " << std::endl;
    }
};
std::ostream& operator<<(std::ostream& s, const std::vector<Base*>& v)
{
    s << "there are " << v.size() << " objects:" << std::endl;
    for(auto& i:v)
        s << i;
    return s;
}

如果你在缝合 (2.) 约普可能会有一个

class Derived3: public Base
{
    ...
    Base* ptrOwnedobject;
};

您可以简单地实现Derived3::out函数以追索到拥有的子项。

如果你处于(3.)的情况,并且你不能容忍同一个对象被打印更多时间,你让不同的父母共享同一个孩子。您应该以某种方式标记子项,以便不会保存两次或更多次,并在完成打印后取消标记所有子项(您很可能会将该机制设置到 Base 类中)。

如果你处于(4.)的情况,那么关于(3.)的说法是强制性的,以避免无限递归。

如果您遇到这种情况(5.),您还必须发现成员属于哪个对象(不是微不足道的)。如果您还有多个继承和虚拟基地...事情甚至更加复杂。