7

以下是代码外观的粗略示例,问题是我如何让 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;
}
4

3 回答 3

16

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

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

#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
于 2012-08-10T18:37:59.803 回答
6

你可以这样做:

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);
}

然后,在应用operator<<on时Base,它会调用派生的输出方法。

于 2012-08-10T16:35:12.357 回答
2

就您的问题描述而言,您有一组动态分配的多态对象。

解决方案可以根据这些对象如何遵循某些设计规则而改变:

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

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

您不能做的第一件事是Base用作值参数(例如在您的 , 因为这会创建一个副本(承认您的对象是可复制的:如果它们引用其他对象,副本会做什么?乘以路径?将引用的对象克隆为好?)

如果您处于情况 (1.),您只需要一个虚函数std::ostream&,在所有叶子对象中被覆盖。然后你需要一个重载 foroperator<<(std::ostream&, Base*)和另一个 for(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;
}

如果你在 sutuation (2.) yopu 可能会

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

您可以简单地实现该Derived3::out功能以求助于拥有的孩子。

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

如果您处于情况(4.),那么关于(3.)的内容将成为强制性的,以避免无限递归。

如果您处于情况(5.)中,您还必须发现成员属于哪个对象(不是微不足道的)。如果你也有多重继承和虚拟基......事情就更复杂了。

于 2012-08-10T17:16:01.247 回答