Search code examples
c++qtqt-creatordllexportpimpl-idiom

Pointer to implementation (PIMPL) in Qt


I made a Dll with MSVS and used pimpl method successfuly exactly like below:

Dll include file:

#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;
    };
}

The private include file:

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

And the cpp file:

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

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

Now I want to implement such method in Qt creator. What changes I should make?

(I guess I have to use QScopedPointer instead of unique_ptr but what is best implementation form for that?)

PS: I set clang as compiler.


Solution

  • Using QScopedPointer I managed to make it work like the way I wanted:

    Dll include:

    #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;
    };
    }
    

    Private include:

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

    The CPP file:

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

    Yet if somebody thinks there is a better idea, please share.