我刚刚尝试了这段代码:
struct FaceOfPast
{
virtual void Smile() = 0;
};
struct FaceOfFuture
{
virtual void Smile() = 0;
};
struct Janus : public FaceOfPast, public FaceOfFuture
{
virtual void Smile() {printf(":) ");}
};
...
void main()
{
Janus* j = new Janus();
FaceOfFuture* future = j;
FaceOfPast* past = j;
future->Smile();
past->Smile();
delete j;
}
它按预期工作(输出两个笑脸),但我认为它甚至不应该编译,重新声明Smile()
是Janus
模棱两可的。
它如何(以及为什么)起作用?