C++ (错误C4275)如何在静态库中创建接口类

发布于 2025-02-09 18:22:26 字数 1142 浏览 2 评论 0原文

我有一个静态库,该库包含一些纯粹的虚拟接口,这些接口与我的大多数DLL共享。

class IBase
{
public:
    //Cannot be virtual or child destructors will not be called
    virtual ~IBase() {} //<- This guy causes my issues
 
    virtual void someFunction() = 0;
}

现在,我有了使用此接口的DLL,并让预处理器定义为解决,如果需要导出,

#ifdef MYDLL_EXPORTS
    #define MYDLL_API __declspec(dllexport)
#else
    #define MYDLL_API
#endif

class MYDLL_API Derived : public IBase{
public:
    Derived(){}
    ~Derived() override{}
    void someFunction() override {}
}

则会生成编译器警告“ C4275 non-dll-dll-dll-Interface class_1'class_1'class_1'用作DLL-Interface class'class'class_2'的基础a href =“ https://learn.microsoft.com/en-us/cpp/eror-messages/compiler-warnings/compiler-warning-level-level-level-2-c4275?view = mmsvc-170” > https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-level-level-level-2-c4275?view =

mmsvc-170 ) t在我的界面上添加了一个具体的破坏者函数,我不会遇到此错误,但是我会泄漏出筛分的内存。

我想知道什么:

  • 解决这个问题的正确解决方案是,
  • 如果不将修复程序作为旁注的情况下实际上出了问题,

这只是我可以用Pragma删除的警告,但是我真的不喜欢这样做't 100%明白为什么要抱怨。

I have a static library that houses some purely virtual interfaces that is shared with most of my DLL's.

class IBase
{
public:
    //Cannot be virtual or child destructors will not be called
    virtual ~IBase() {} //<- This guy causes my issues
 
    virtual void someFunction() = 0;
}

Now I have Dll's that use this interface and have preprocessor defines to worked out if they need to be exported

#ifdef MYDLL_EXPORTS
    #define MYDLL_API __declspec(dllexport)
#else
    #define MYDLL_API
#endif

class MYDLL_API Derived : public IBase{
public:
    Derived(){}
    ~Derived() override{}
    void someFunction() override {}
}

This generates compiler warning "C4275 non - DLL-interface class 'class_1' used as base for DLL-interface class 'class_2'" (https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170)

Now if I hadn't added a concrete destructor function on my interface I would not have gotten this error, but I would have leaked Derives' memory like a sieve.

I'm wondering what:

  • The correct fix for this problem is
  • What could actually go wrong without making the fix

A side note is it is just a warning I could remove with pragma, but I really don't like doing this especially if I don't 100% understand why it is complaining.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文