【问题标题】:How to call DLL's virtual function from executable? C++ VS2019如何从可执行文件中调用 DLL 的虚函数? C++ VS2019
【发布时间】:2020-07-16 22:53:31
【问题描述】:

我正在编写自己的游戏编辑器,它包含许多 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 中创建这个指针并将它们传递给应用程序。我只是想给你一个想法。

【问题讨论】:

    标签: c++ pointers dll architecture


    【解决方案1】:

    这并没有什么问题,又是一个 VS 调试器的问题。删除一些功能有效!

    【讨论】:

      猜你喜欢
      • 2017-04-27
      • 2015-10-17
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多