gpt4 book ai didi

c++ - 在 C++ 中导出没有基类的父类

转载 作者:太空狗 更新时间:2023-10-29 20:46:24 28 4
gpt4 key购买 nike

给定类 FooBar,其中 Bar 派生自 Foo,我如何导出 Bar 而不必导出 Foo ?


Foo.h

class Foo
{
public:
Foo();
virtual bool Read(...);
virtual bool Write(...);
~Foo();
};


Bar.h

#ifdef BUILD_DLL
# include "Foo.h"
# define EXTERNAL __declspec(dllexport)
#else
# define EXTERNAL __declspec(dllimport)
#endif

class EXTERNAL Bar: public Foo
{
public:
Bar();
bool Read(...);
bool Write(...);
~Bar();
};



编辑

这是 Visual C++ 2010 给我的警告:

warning C4275: non dll-interface class 'Foo' used as base for dll-interface class 'Bar'



编辑 #2

以下是我为解决问题所做的工作。我将 FooBar 分开了 DLL。客户端应用程序链接到 Bar.dll,Bar.dll 链接到 Foo.dll。这对我想要实现的目标很有效...

最佳答案

这还不行吗?它应该。仅仅因为您没有显式导出类并不意味着逻辑不在二进制文件中。这些符号对任何导入该类的东西都不可见。

您的解决方案应该可以做到。您遇到链接错误还是什么?

编辑:我刚刚看到您在一种情况下只包含 Foo 的 header 。你不需要这样做:

#ifdef BUILD_DLL
# define EXTERNAL __declspec(dllexport)
#else
# define EXTERNAL __declspec(dllimport)
#endif

#include "Foo.h"

class EXTERNAL Bar: public Foo
{
public:
Bar();
bool Read(...);
bool Write(...);
~Bar();
};

这样你就不能在不同的模块中使用 Foo,因为它没有导出,但你可以使用和声明 Bar

关于c++ - 在 C++ 中导出没有基类的父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8301747/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com