将多态类数据写入文件?
所以我有这些课程。有一个基类,但它具有/将具有大量派生类,并且这些派生类也将能够具有派生类。我希望能够有一个函数将二进制数据写入文件,但我不确定如何使用大量派生类来执行此操作。
我在想:
void writeData(ofstream & _fstream)
{
_fstream.write()//etc..
}
但是,每个实现此方法的派生类都必须写入其父类的所有数据,这将重复大量代码。
在不重写所有先前编写的 writeData()
代码的情况下执行此操作的最佳方法是什么?
So I have these classes. There's one base class, but it has/will have lots and lots of derivatives, and those derivative classes will be able to have derivatives as well. I'd like to be able to have a function that writes their binary data to a file, but I'm not sure how to do this with lots and lots of derived classes.
I was thinking something along the lines of:
void writeData(ofstream & _fstream)
{
_fstream.write()//etc..
}
But then each derived class that implemented this method would have to write all of it's parent class's data, and that would be duplicating a lot of code.
What's the best way to do this without rewriting all of the previously written writeData()
code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从派生类实现调用基类实现:
You can call the base class implementation from the derived class implementation:
派生类可以调用基
write
方法以避免代码重复。事实上,如果某些父母的数据是私有的但仍然被间接使用,这可能是唯一的方法。Derived class can call base
write
methods to avoid code duplication. In fact, that may be the only way to go if some parent's data is private but still is indirectly used.如果您想避免重新设计序列化函数的所有派生类的实现,您可以朝另一个方向,从基类到派生类:
在您的基类中提供一个非
虚拟
函数来启动序列化过程。客户端代码通过指针(或引用)调用此函数。还提供一个虚函数来为子类进行序列化。从基类的Serialize
函数调用该函数。(编辑)如果您想提供用于序列化子类的默认功能,但仍然希望能够为特定情况提供专门的功能,那么序列化子类的函数不必是纯虚拟的。然而,通过阅读你的OP,在我看来,每个子类都需要提供此功能。为了模拟该需求,我在这里将
DoSerialize
函数设置为纯虚拟函数。例子:
If you want to avoid re-engineering all the derived class' implementation of the serialization functions, you can go in the other direction, from the base to the derived classes:
In your base class provide a non-
virtual
function to start the serialization process. Client code calls this function via a pointer (or reference). Also provide a virtual function that does the serialization for the subclass. Call that function from the base class'Serialize
function.(EDIT) If you want to provide default functionality for serializing the subclasses, but still want to be able to provide specialized functionality for specific cases, then the function that serializes the subclasses need not be pure virtual. However, by my reading of your OP it seemed to me that every subclass would need to be required to provide this functionality. To model that requirement, I have made the
DoSerialize
function pure virtual here.Example:
最终,每个派生类都有责任确保它已正确序列化。派生类可能需要在基类之前或之后序列化一些数据,具体取决于其用途。它可能还想完全覆盖基类数据的序列化方式。
这样看——这里执行的功能是序列化和反序列化。这里的关键是它需要正确执行。因此,唯一有能力做到这一点的班级是拥有完整知识的班级。换句话说,它是您的派生类。
因此,有时您必须调用 Base::writeData(),但是否这样做应该完全取决于派生类。请记住,您希望类层次结构满足一些基本设计原则。一旦你掌握了这一点,它应该相对容易。
Ultimately, it is the responsibility of each derived class to make sure that it has been serialized properly. A derived class may need to serialize some data before or after the base class, depending on its purpose. It may also want to totally override the way the base class data is serialized.
Look at it this way - the function being performed here is serialization and de-serialization. The critical thing here is that it needs to be performed correctly. Therefore, the only class that is in a good position to do this is the one with complete knowledge. In other words, its your derived class.
So, there are times when you will have to call Base::writeData(), but whether or not you do that should be left totally up to the derived class. Remember, what you want is for your class hierarchy to satisfy some basic design principles. Once you've got that, it should be relatively easy.