简体   繁体   English

虚拟函数歧义解决

[英]Virtual function ambiguity solving

So here I got a descent explanation that what actually is a virtual function and why do we really need it, 因此,在这里我得到了一个血统的解释,即什么是虚函数以及为什么我们真正需要它,

In C++, what is a virtual base class? 在C ++中,什么是虚拟基类?

But there still remains a question in my mind that how compiler actually interpret that which definition should be used in case of multiple or hybrid inheritance. 但是我仍然有一个疑问,即在多重继承或混合继承的情况下,编译器实际上如何解释应使用哪个定义。 For example consider this example 例如考虑这个例子

class Up {
 public: 
 void func(){
      ...
    } 
};

class Middle_1 : public virtual Up {
 public:
 ...
};

class Middle_2 : public virtual Up {
 public:
 ...
};

class Down : public Middle_1, public Middle_2 {
 public:
 ...
};

In above example code, class Down is receiving two definition of class Up (ie one from Middle_1 and another from Middle_2 ). 在上面的示例代码中,类Down正在接收类Up两个定义(即,一个来自Middle_1 ,另一个来自Middle_2 )。 However we had ensured to use the virtual tag in Middle_1 and Middle_2 class which will remove the case of ambiguity (because compiler will take only one definition) and that is where my question arises. 但是,我们确保在Middle_1Middle_2类中使用virtual标记,这将消除歧义的情况(因为编译器仅采用一个定义),这就是我的问题所在。

Actually I am having a bunch of questions, 其实我有很多问题,

  • How will compiler select the best suited class? 编译器将如何选择最合适的类?

  • Will the selection be similar in all conditions? 在所有情况下选择都会相似吗?

  • Didn't it contradict the fact that compiler have zero IQ? 这是否与编译器的IQ为零相矛盾?

Thanks in advance. 提前致谢。

There's only one Up class in the entire program. 整个程序中只有一个Up No need to select anything. 无需选择任何内容。

Perhaps you wanted to ask how will compiler select the best suited Up subobject ? 也许您想问一下编译器将如何选择最适合的Up 子对象 Ostensibly there's an Up subobject in each Middle_1 object, and an Up subobject in each Middle_2 subobject. 表面上,每个Middle_1对象中都有一个Up子对象,而每个Middle_2子对象中都有一个Up子对象。 The compiler will need to select one of them to use with Down , right? 编译器将需要选择其中之一与Down一起使用,对吗?

Wrong. 错误。 With virtual inheritance, subobjects are not fixed in place relatively to their owners. 通过虚拟继承,子对象不会相对于其所有者固定在适当的位置。 Neither Middle_1 nor Middle_2 create their own Up subobjects, and they do not know in advance where these Up subobjects live. Middle_1Middle_2都不会创建自己的Up子对象,并且他们事先不知道这些Up子对象在哪里。 They only know they are going to have one. 他们只知道他们将要拥有一个。 Somebody will have to hand them down an instance of Up . 有人将不得不把它们传给Up实例。

So the compiler will do a little dirty trick. 因此,编译器会做一些卑鄙的把戏。 It will create a single Up subobject for each Down object, and then tell the Middle_1 part and the Middle_2 part and the Down part that it is their Up subobject. 它将为每个Down对象创建一个单独的 Up子对象,然后告诉Middle_1部分 Middle_2部分 Down部分是它们的 Up子对象。 So all three parts will treat that single Up as their own, completely oblivious to the fact that it is in fact shared. 因此,所有三个部分都将把这个Up当作自己的Up ,完全忽略了事实上它是共享的。

This scheme did require some non-trivial IQ to invent , but every dumb compiler out there is capable of implementing it. 这个方案确实需要一些非平凡的IQ来发明 ,但是那里的每个愚蠢的编译器都能够实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM