虚函数动态绑定问题 [英] Virtual function dynamic binding issue
问题描述
我陷入了继承和多态的实践项目,我们被要求通过编码派生的struct xhost来扩展基础结构主机。
我们不允许修改host.h:
I'm stuck in a practice project on inheritance and polymorphism, where we were asked to expand the base struct host by coding its derived struct xhost.
We were not allowed to modify host.h:
#ifndef HOST_H
#define HOST_H
#include <string>
#include <iostream>
struct host {
/////////snap//////////
private:
virtual void handle_recv(const host&, const std::string&) const {};
};
#endif
和我的xhost看起来像这样(部分)1.cpp:
And my xhost looks like this in (part of) 1.cpp:
#include "host.h"
using namespace std;
struct xhost : public host
{
virtual void send(const host &other, const string &s) const
{
string message = s;
for (int i = 0; i != splugList.size(); i++)
{
message = (*(splugList[i]))(message);
}
cout << "[" << name << " -> " << other.name << "] " << message << std::endl;
other.handle_recv(*this, s);//////////////////////ERROR HERE
}
/////////snap//////////
private:
virtual void handle_recv(const host &other, const string &s) const
{
string message = s;
for (int i = 0; i != rplugList.size(); i++)
{
(*(rplugList[i]))(other, *this, message);
}
}
};
编译器说我试图访问私有成员函数基类主机中的handle_recv,这当然是一个错误。但是,我不明白为什么要访问基类函数;我认为,因为handle_recv是一个虚函数而另一个是引用,它会动态绑定到xhost中定义的handle_recv而不是主机中的那个?
The compiler said I was trying to access private member function handle_recv in base class host, which is of course an error. However, I don't understand why it was base class function being accessed; I thought that since handle_recv is a virtual function and other is a reference, it would dynamically bind to handle_recv defined in xhost but not the one in host?
推荐答案
好的我我想我现在明白了...不完全理解实践项目的规范(并没有注意到它)是我的坏事。规格很长,所以我没有将所有这些都包含在我的问题中,这也导致了进一步的混淆。
无论它值多少我都会尝试记录这个混乱的原因:
xhost 中定义的发送功能应该是:
1.在/// ERROR行上方执行操作后,检查参数引用其他是否绑定到主机或 xhost 。
2.如果其他绑定到 xhost :调用其他(即xhost)的成员函数handle_recv。
如果其他绑定到主机: DO NOTHING 。 (f%^& *%
Okay I think I figure it out now... It was my bad for not fully understanding the specifications of the practice project (and not noticing it). The specs were very long, so I didn't include all of them in my question, which also led to further confusions.
For whatever it's worth I'll try to document the cause of this mess:
What send function defined in xhost was expected to do was:
1. After doing stuff above the ///ERROR line, check whether the argument reference other is bound to a host or an xhost.
2. If other is bound to an xhost : call other(i.e. an xhost)'s member function handle_recv.
And if other is bound to a host : DO NOTHING. (f%^&*%
## @ ##
me ....)
所以Philippe Mori所说的是正确的,但与这里的混乱无关(这是100%我的坏。抱歉......)。 解决方案可以简单地用以下内容替换/// ERROR行:
me....)
So what Philippe Mori said was correct, but not really related to the mess here (which is 100% my bad. Sorry...). The "solution" could simply be replacing the ///ERROR line by something like:
if (typeid(other) == typeid(const xhost &))
dynamic_cast<const xhost="">(other).handle_recv(*this, message);
这篇关于虚函数动态绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!