当虚拟机无法正常工作时 [英] When virtual doesn't work
问题描述
此刻,我的C ++类中出现一个奇怪的错误.我有一个ActiveX包装器类(作为wxWidgets的一部分),我在其中添加了新的虚函数.我有另一个从ActiveX继承的类(wxIEHtmlWin),但是ActiveX类总是调用自己的函数,而不是wxIEHtmlWin中的函数来覆盖它.
I have a weird error in my C++ classes at the moment. I have an ActiveX wrapper class (as part of wxWidgets) that i added a new virtual function to. I have another class that inherits from the ActiveX one (wxIEHtmlWin) however the ActiveX class always calls its own function instead of the one in wxIEHtmlWin which overrides it.
我不知道为什么会这样.我将函数设为纯虚拟函数,现在程序执行函数调用时崩溃,但否则编译正常.有什么方法可以禁用虚拟功能,还是我在Visual Studio中发现了错误?
I can't work out why this is happening. I made the function pure virtual and now the program crashes when it does the function call but compiles fine otherwise. Is there any way to disable virtual functions or have I found a bug in Visual Studio?
ActiveX类
protected:
virtual FrameSite* getNewFrameSite()=0;
wxIEHtmlWin类
wxIEHtmlWin class
class wxIEHtmlWin : public wxActiveX
{
protected:
FrameSite* getNewFrameSite();
}
FrameSite* wxIEHtmlWin::getNewFrameSite()
{
return new gcFrameSite(this);
}
我添加了另一个测试功能(返回一个int),但仍然无法解决.
I've added another test function (returns an int) and still screws up.
有问题的代码链接: http://lodle.net/public/iebrowser.rar
好的,感谢下面的回答,我开始工作了.我所做的是分两部分创建activex类(如建议的那样),但是在wxIEHtmlWin中,我在构造函数代码中调用了第二部分.像这样:
OK thanks to the answer below i got it to work. What i did was create the activex class in two parts (like suggested) however in wxIEHtmlWin i called the second part in the constructor code. Like so:
wxIEHtmlWin::wxIEHtmlWin(wxWindow * parent, wxWindowID id, const wxPoint& pos,const wxSize& size,long style, const wxString& name) : wxActiveX()
{
wxActiveX::Create(parent, PROGID, id, pos, size, style, name);
SetupBrowser();
}
现在我知道为什么wxWidgets支持两部分构造.
Now i know why wxWidgets supports two part construction.
推荐答案
您正在从类的构造函数中调用虚拟方法(通过另一个调用).由于尚未构建子类,因此将在当前类上调用该方法.解决方法是使用init()方法,并在构造类后调用它.
You are calling the virtual method from within the class's constructor (via another call). This will call the method on the current class as the sub-class hasn't been constructed yet. The fix is to use an init() method and call it after constructing the class.
即类似这样的
class wxActivex {
wxActivex() {}
virtual void init() {
getNewFrame();
}
};
// in the code that uses these classes:
wxActivex *activex = new IEHtmlFrame();
activex->init();
这篇关于当虚拟机无法正常工作时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!