【问题标题】:Skip Unused Virtual Function跳过未使用的虚拟功能
【发布时间】:2015-05-09 12:04:50
【问题描述】:

我有主要的可执行文件和两个取消引用到 DLL 的函数。

class CPluginInterface
{
public:
    virtual void A(void) = 0;
    virtual void B(void) = 0;
};

我是这样创建的 DLL

//Main.h
#include "Function.h"

class CForward : public CPluginInterface
{
public:
    //void A(void);
    void B(void);
};

//Main.cpp
#include "Main.h"

/*void CForward::A(void)
{
    //A Function is commented because it is not used
}*/

void CForward::B(void)
{
    //Do something here
}

extern "C"
{
    // Plugin factory function
    //void __declspec(dllexport) __cdecl A(void) { }
    void __declspec(dllexport) __cdecl B(void) { }
}

但是程序崩溃了,因为当主可执行文件取消引用它时 A(void) 不存在。如何跳过 A(void)?

如果我像这样创建 DLL,它可以正常工作。

//Main.h
#include "Function.h"

class CForward : public CPluginInterface
{
public:
    void A(void);
    void B(void);
};

//Main.cpp
#include "Main.h"

void CForward::A(void)
{
    //Do something here
}

void CForward::B(void)
{
    //Do something here
}

extern "C"
{
    // Plugin factory function
    void __declspec(dllexport) __cdecl A(void) { }
    void __declspec(dllexport) __cdecl B(void) { }
}

注意:我创建插件接口。

【问题讨论】:

    标签: c++ plugins interface null dereference


    【解决方案1】:

    接口虚函数上的 =0 后缀表示它们是纯虚函数,并且在从基类继承时需要覆盖它们。在您的第一个示例中,CForward 是一个抽象类,因为您没有覆盖 A,因此您无法创建 CForward 的实例。

    https://stackoverflow.com/a/2652223

    【讨论】:

    • 太棒了,大卫。如果您对答案进行投票并单击复选标记,我将获得一些声誉积分,当其他人搜索此主题时,您的问题将显示为已回答。
    • 哦,对不起。我不知道。完毕。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2019-07-28
    相关资源
    最近更新 更多