首页 \ 问答 \ 虚拟和非虚拟继承的混合(mixture of virtual and non-virtual inheritance)

虚拟和非虚拟继承的混合(mixture of virtual and non-virtual inheritance)

在尝试更深入地分析C ++的继承机制时,我偶然发现了以下示例:

#include<iostream>

using namespace std;

class Base {
public:
    virtual void f(){
    cout << "Base.f" << endl; 
    }
};

class Left : public Base { //NOT VIRTUAL!!!
public:
void g(){ 
        f();
    }     
};

class Right : public Base{
public:
    virtual void f(){
    cout << "Right.f" << endl; 
    }
};

class Bottom : public Left, public Right{
public:
    Bottom(int arg){ }
    //void f() { }
};

int main(int argc,char **argv)
{
    Bottom* b = new Bottom(23);
    b->g();
}

很明显,打电话

b->f()

是不明确的,所以对象底部没有唯一的方法f() 。 现在,打电话

b->g()

工作得很好,打印

Base.f

好吧,据我所知:

  1. static类型是Bottom,所以我们称之为g()方法,因为它是非虚拟的
  2. g()方法是从Left继承的,所以我们称之为继承方法
  3. 现在,左边的g()尝试调用虚方法f() 。 根据C ++ sepcification,我们调用f()方法的动态类型的指针(这是底部)

但是底部没有方法f() ......至少不是唯一的方法。 为什么这个程序执行Left::Base::f()而不是Right::Base::f()或者为什么它只是没有说明调用f()与Bottom不一致?


while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon the following example:

#include<iostream>

using namespace std;

class Base {
public:
    virtual void f(){
    cout << "Base.f" << endl; 
    }
};

class Left : public Base { //NOT VIRTUAL!!!
public:
void g(){ 
        f();
    }     
};

class Right : public Base{
public:
    virtual void f(){
    cout << "Right.f" << endl; 
    }
};

class Bottom : public Left, public Right{
public:
    Bottom(int arg){ }
    //void f() { }
};

int main(int argc,char **argv)
{
    Bottom* b = new Bottom(23);
    b->g();
}

It is clear that calling

b->f()

is ambiguous, so there is no unique method f() on object Bottom. Now, calling

b->g()

works fine and prints

Base.f

Well, as far as I see this:

  1. static type is Bottom, so we call its g() method, as it is non-virtual
  2. g() method is inherited from Left, so we call this inherited method
  3. Now, g() in Left tries to call virtual method f(). According to C++ sepcification, we call f() method of a dynamic type of our pointer (which is Bottom)

BUT Bottom does not have method f()... at least not a unique one. Why does this program executes Left::Base::f() rather than Right::Base::f() or why does it simply not states that call to f() is ambiguous from Bottom?


原文:https://stackoverflow.com/questions/10166313
更新时间:2022-03-26 21:03

最满意答案

您的代码中有两个不同的控件名称。 事件附加到webViewVideoPlayer ,稍后在名为webBrowserVideoPlayer.的其他WebView调用Navigate webBrowserVideoPlayer.

我使用WebView进行了测试,并按预期调用了事件。


You've got two different control names in your code. The events are attached to webViewVideoPlayer, yet later the Navigate is called on a different WebView called webBrowserVideoPlayer.

I did a test with a WebView and the events were called as expected.

相关问答

更多
  • 您将需要使用ScriptNotify事件。 以下是我处理场景的方式(使用NavigateToString)。 如果您从URL中检索Web视图内容,则需要修改HTML才能使其工作。 将以下Javascript代码添加到您的HTML中