使用非虚拟的虚拟继承函数?
我在尝试实现游戏中某些类的序列化功能时遇到了麻烦。我将一些数据存储在原始文本文件中,并且希望能够保存和加载它。 然而,这细节无关紧要。问题是我试图使保存文件感兴趣的每个对象能够序列化自身。为此,我定义了一个接口 ISerialized,其中包含纯虚拟的操作符<< 声明。和运算符>>。
类层次结构看起来像这样。
-> GameObject -> Character -> Player ...
ISerializable -> Item -> Container ...
-> Room ...
这意味着序列化不同类的对象有很多可能的情况。例如,容器应该调用operator<<在所有包含的物品上。
现在,由于运算符>>是虚拟的,我想如果我想序列化一些实现 ISerialized 中定义的功能的东西,我可以做类似的事情
ostream & Player::operator<<(ostream & os){
Character::operator<<(os);
os << player_specific_property 1 << " "
<< player_specific_property 2 << "...";
return os;
}
,
ostream & Character::operator<<(ostream & os){
GameObject::operator<<(os);
os << character_specific_property 1 << " "
<< character_specific_property 2 << "...";
return os;
}
但我很快了解到这第一次尝试是非法的。我在这里要问的是如何解决这个问题?
我不想为每个类手动实现一个功能。我想我正在寻找类似于 Java 的 super
功能的东西。
任何帮助表示赞赏。
-- 编辑评论 ------------
好吧,上次我写问题的时候很着急。现在的代码更像是我尝试编译它时的代码。我解决了这个问题,我遇到的问题与提出的问题无关。我很惭愧地说这是由于代码大量重构后的错误引起的,而且该运算符并未在每个基类中实现。
不过非常感谢您的回复!
I have run into trouble trying to implement functionality for serializing some classes in my game. I store some data in a raw text file and I want to be able to save and load to/from it.
The details of this, however, are irrelevant. The problem is that I am trying to make each object that is interesting for the save file to be able to serialize itself. For this I have defined an interface ISerializable, with purely virtual declarations of operator<< and operator>>.
The class Hierarchy looks something like this
-> GameObject -> Character -> Player ...
ISerializable -> Item -> Container ...
-> Room ...
This means there are many possible situations for serializing the objects of the different classes. Containers, for instance, should call operator<< on all contained items.
Now, since operator>> is virtual, i figured if I wanted to serialize something that implements the functionality defined in ISerializable i could just do something like
ostream & Player::operator<<(ostream & os){
Character::operator<<(os);
os << player_specific_property 1 << " "
<< player_specific_property 2 << "...";
return os;
}
and then
ostream & Character::operator<<(ostream & os){
GameObject::operator<<(os);
os << character_specific_property 1 << " "
<< character_specific_property 2 << "...";
return os;
}
but I quickly learnt that this first attempt was illegal. What I'm asking here is how do I work around this?
I don't feel like implementing a function manually for each class. I guess I'm looking for something like the super
functionality from Java.
Any help is appreciated.
-- COMMENTS ON EDIT ------------
Alright, last time I was in a hurry when I was writing the question. The code is now more like it was when I tried to compile it. I fixed the question and the problem I had was unrelated to the question asked. I'm ashamed to say it was caused by an error in the wake of a large refactoring of the code, and the fact that the operator was not implemented in every base class.
Many thanks for the replies however!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于您尝试非虚拟地调用虚拟函数。问题出在这一行:
os = Character::operator<<(os);
。这是一个赋值,但std::ostream
没有operator=
。无论如何,你不需要任务。返回的流与您传入的流相同。返回它的唯一原因是您可以链接它们。
因此解决方法是将代码更改为
The problem is not in your attempt to call a virtual function non-virtually. The problem is this line:
os = Character::operator<<(os);
. That is an assignment, butstd::ostream
doesn't have anoperator=
.You don't need the assignment anyway. The stream returned is the same stream as the stream you pass in. The only reason it's returned is so you can chain them.
Hence the fix is to just change the code to
这不是重载运算符 << 的方式。对于 ostream 作品。左边的操作符是一个ostream(因此你必须将它重载为一个自由函数),右边的操作符是你的对象(这就是虚拟机制不容易工作的原因。
我想你可以尝试:
现在是一个派生的类自然可能会调用其父级的输出方法:
This is not how overloading operator<< for ostream works. The left-hand operator is an ostream (hence you gotta overload it as a free function) and the right-hand operator is your object (which is why the virtual mechanism wouldn't easily work.
I suppose you could try:
Now a derived class naturally might call the output method of its parent(s):