C++ 从DLL导出正向声明的类

C++ 从DLL导出正向声明的类,c++,class,dll,dllexport,C++,Class,Dll,Dllexport,我想从DLL导出一个类。它使用Pimpl惯用语,如: #ifdef THING_EXPORT #define THING_API __declspec(dllexport) #else #define THING_API __declspec(dllimport) #endif class thing_impl; class THING_API thing { public: ... private: thing_impl* pimpl_; }; thing\u impl的

我想从DLL导出一个类。它使用Pimpl惯用语,如:

#ifdef THING_EXPORT
#define THING_API __declspec(dllexport)
#else
#define THING_API __declspec(dllimport)
#endif

class thing_impl;

class THING_API thing
{
public:
    ...
private:
    thing_impl* pimpl_;
};
thing\u impl
的构造函数或析构函数(或任何方法)在该DLL之外将不可用。我是否必须将
THING\u API
放在前向声明的名称
THING\u impl
前面

我是否必须将
THING\u API
放在前向声明的名称
THING\u impl
前面

这要视情况而定,但鉴于pimpl习语的性质,不需要导出它

对于整个项目,您可能会收到关于未导出的警告和错误,或者将其限制在成员变量的范围内(如下所示)

在实现过程中考虑的其他因素是在主<代码>事物类内声明“pIMPL”实现类<代码> THIGIMPIL> <代码>,以进一步限制潜在访问。

class THING_API thing
{
public:
    thing(const thing&);
    thing& operator=(const thing&);
    thing(thing&&); // if needed
    thing& operator=(thing&&); // if needed
    ~thing();
    ...
private:
    class thing_impl;
    #pragma warning(push)
    #pragma warning(disable: 4251)
    thing_impl* pimpl_;
    #pragma warning(pop)
};

需要注意的一点是从DLL导出类时,将导出整个类,因此请确保适当的复制构造函数、复制赋值运算符和析构函数(如上所述)已存在并已实现(如果需要,还将添加移动构造函数)。如果它们不在那里,编译器将生成它们,并且由于使用了
pimpl\uu
成员,它们很可能是不正确的。为此,您还可以考虑使用<代码> STD::SysDypPTR 来帮助管理<代码> PMPYX。

我期望一个<代码> UNQuyYPPTR <代码>更适合保存pIMPL。
class THING_API thing
{
public:
    thing(const thing&);
    thing& operator=(const thing&);
    thing(thing&&); // if needed
    thing& operator=(thing&&); // if needed
    ~thing();
    ...
private:
    class thing_impl;
    #pragma warning(push)
    #pragma warning(disable: 4251)
    thing_impl* pimpl_;
    #pragma warning(pop)
};