Qt中的实现指针(PIMPL)

问题描述 投票:0回答:2

我用MSVS制作了一个Dll,并成功使用pimpl方法,如下所示:

包含文件:

#include <memory>

#define DllExport __declspec( dllexport ) 

namespace M
{
    class P
    {
    public:
        DllExport P(/*some arguments*/);
        DllExport ~P();
        DllExport int aFunction (/* some arguments*/);

    private:
        class C;
        std::unique_ptr<C> mc;
    };
}

私有包含文件:

namespace M
{
    class P::C
    {
        public:
         # Other variables and functions which are not needed to be exported...
    }
}

和cpp文件:

DllExport M::P::P(/*some arguments*/):mc(std::make_unique<C>())
{
# ...
}
DllExport M::P::~P(){ }

DllExport int M::P::aFunction (/* some arguments*/)
{
#...
}

现在,我想在Qt Creator中实现这种方法。我应该进行哪些更改?

(我想我必须使用QScopedPointer而不是unique_ptr,但是什么是最佳实现形式?]

PS:我将clang设置为编译器。

c++ qt qt-creator dllexport pimpl-idiom
2个回答
1
投票
您的代码看起来像find,它应该独立于IDE。您可以使用this,但我认为this是您要搜索的。

0
投票
使用QScopedPointer我设法使其按我想要的方式工作:

包含以下内容:

#define DllExport __declspec( dllexport ) #include <QScopedPointer> namespace M{ class PPrivate; class P { public: DllExport P(/*some arguments*/); DllExport ~P(); DllExport int aFunction (/* some arguments*/); private: Q_DECLARE_PRIVATE(P) QScopedPointer<PPrivate> d_ptr; }; }

私人包括:

namespace M { class PPrivate { public: # Other variables and functions which are not needed to be exported... } }

CPP文件:

DllExport M::P::P(/*some arguments*/) :d_ptr(new PPrivate()) { #... } DllExport M::P::~P(){ } DllExport int M::P::aFunction (/* some arguments*/) { #... }

但是,如果有人认为有更好的主意,请分享。
© www.soinside.com 2019 - 2024. All rights reserved.