c++ - 从 iostream 读取父类(super class)的子类实例。 >> 运算符如何知道哪个子类?

标签 c++ iostream

我有一个带有多个子类的父类(super class) Element,我们称它们为 A 和 B。我想重载 << 和 >>,以便可以保存和加载我的对象。

class Element
{
public:
   Element();
   int superProperty;
   virtual void write(iostream &out)
   {
      out << superProperty;
   }
   virtual void read(iostream &in)
   {
      in >> superProperty;
   }
};

iostream operator<<(iostream &out, const Element &elt)
{
  elt.write(out);
  return(out);
}

iostream operator>>(iostream &in Element &elt)
{
  elt.read(in);
  return(in);
}

class A : public Element
{
public:
  A();
  int subProperty;
   void write(iostream &out)
   {
      Element::write(out);
      out << subProperty;
   }
   void read(iostream &in)
   {
      Element::read(in);
      in >> subProperty;
   }
};

class B : public Element
{
public:
  B();
  double subProperty;
   void write(iostream &out)
   {
      Element::write(out);
      out << subProperty;
   }
   void read(iostream &in)
   {
      Element::read(in);
      in >> subProperty;
   }
};

有了这些定义,我可以轻松地写出我的元素文件,将每个元素写为

iostream mystream;
Element e;
...
mystream << e;

我陷入困境的是读回它们。我希望它看起来像这样:

iostream mystream;
Element *pe;
...
pe = new Element();  // the problem is right here
mystream >> *pe;

但这行不通,因为我不知道我要读取的元素是 Element 还是 A 还是 B。(在我的应用程序中,我从未真正实例化 Element。所有对象都是一个的子类。) 我求助于写出一个字符来指示类别...

if (dynamic_cast<A>(e))
{
   out << 'A';
   out << e;
} else if (dynamic_cast<B>(e))
{
  out << 'B';
  out << e;
}

然后切换/大小写如下:

    char t;
    Element *pe;
...
    in >> t;
    switch (t)
    {
      case 'A':
        pe = new A;
        break;
      case 'B':
        pe = new B;
        break;
    }
    in >> *pe;

但是看起来不太优雅。

流式传输不同对象的更好方法是什么?

最佳答案

本质上,这就是任何序列化解决方案都会归结为的。虽然优雅性可能会有所提高,但使用代码生成可能仍然更好(序列化框架可以做到这一点)。

使用虚函数或映射(type_index 到标记 1 )绝对可以避免动态转换。该开关也可以用 map (工厂标签)替换。甚至可以(使用一些模板魔法)使用相同的代码来初始化两个 map ,例如:

using Factory = void(*)();
struct SerializationInfo {
  char key;
  type_index type;
  Factory factory;
};

template <class T>
SerializationInfo Serializable(char key) // note that SerializationInfo is not a template!
{
    return {key, typeid(T), []() { return new T(); }}; // IIRC captureless lambda is convertible to a function pointer
}

Maps buildSerializationMaps(initializer_list<SerializationInfo>);

buildSerializationMaps({
    Serializable<A>('A'),
    Serializable<B>('B'),
});

其中 Serialized 是一个函数模板,它将所有序列化信息( key 、类型 ID 和工厂函数)包装在标准接口(interface)中。

关于c++ - 从 iostream 读取父类(super class)的子类实例。 >> 运算符如何知道哪个子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69067872/

相关文章:

c++ - 如何更新文件中特定行的数据?

c++ unget on fstreams是否安全

c++ - 输入文件名的文件流

C++ 事件库

c++ - 使用 CMake 包含共享库 (.so)

c++ - 更好地理解 getline() 和 cin

c++ - 使用 boost 库的预编译 header 需要很长时间才能编译

c++ - 抽象类中的虚拟类

c++ - $stdin 与 std::istream 的兼容性,使用 swig、C++ 和 Ruby

c++ - 将 fstream ">>"作为函数参数传递