我正在编写自己的游戏编辑器,它包含许多DLL。一些DLL使用其他DLL派生一些类。问题是,每当我使用基类的指针调用虚拟函数(无论是否纯函数,结果相同)时,应用程序就会崩溃。可视化:
//DLL1.h in DLL1
class Base{
static Base* SELF; //I use this to access functions
void DoSometing(); //Defined in DLL1.cpp
virtual void VirtualSomething();
};
//DLL2.h in DLL2
class Derived : public Base{
virtual void VirtualSomething(); //Defined in DLL2.cpp
};
//main.cpp in APP.exe
void main(){
Base::SELF = new Derived;
Base::SELF->DoSomething(); //Works fine
Base::SELF->VirtualSomething(); //Crashes
}
注意:我必须使用Base :: SELF指针,因为我想稍后再使用另一个DLL对其进行更改。在这里创建一个派生的指针并将其设置为Base :: SELF似乎很尴尬,但要复杂得多,我正在另一个DLL中创建此指针并将其传递给应用程序。我只是想给你个主意。
这没什么问题,再次是VS Debugger问题。删除一些有效的功能!