pimpl-idiom

What are the pros and cons of using d-pointers?

两盒软妹~` 提交于 2019-12-03 17:05:02
问题 d-pointers are heavily used in Qt, they are an implementation of pimpl idiom. I know advantages and disadvantages of pimpl idiom. But I have missed the advantages of d-pointers implementation. Here and here are the samples of d-pointers. Isn't it easier to just use this? class MyClassPrivate; class MyClass { // interface methods private: MyClassPrivate *pimpl_; }; 回答1: The set of macros for d-pointer pattern provides some sort convenience and consistencies. For example, Q_DECLARE_PRIVATE

Pimpl idiom using shared_ptr working with incomplete types

只愿长相守 提交于 2019-12-03 15:41:10
I'm reading Effective Modern C++ by Scott Meyers and he's discussing the use of the pimpl idiom and pointing to the implementation class with unique_ptr , but there is an issue of special member functions (such as destructors) requiring the type to be complete. This is because unique_ptr 's default deleter statically asserts whether the type to be deleted is complete, before delete p is used. So any special member functions of the class must be defined in the implementation file (rather than being compiler-generated), after the implementation class has been defined. At the end of the chapter,

std::unique_ptr pimpl in dll generates C4251 with visual studio

假装没事ソ 提交于 2019-12-03 15:04:12
问题 This is not a breaking issue but I like to clean my code from warnings so this is getting on my nerves. I have been using the c++11 version of pimpl idiom to hide the class implementation for my library the usual way. // dll header class FrameworkImpl; class EXPORT_API Framework { Framework(const Framework&) = delete; Framework& operator=(const Framework&) = delete; Framework(Framework&&) = delete; Framework& operator=(Framework&&) = delete; public: Framework(); ~Framework(); private: std:

What are the pros and cons of using d-pointers?

孤者浪人 提交于 2019-12-03 06:04:12
d-pointers are heavily used in Qt, they are an implementation of pimpl idiom. I know advantages and disadvantages of pimpl idiom. But I have missed the advantages of d-pointers implementation. Here and here are the samples of d-pointers. Isn't it easier to just use this? class MyClassPrivate; class MyClass { // interface methods private: MyClassPrivate *pimpl_; }; The set of macros for d-pointer pattern provides some sort convenience and consistencies. For example, Q_DECLARE_PRIVATE ensures that the pimpl private class for Foo is named as FooPrivate, that FooPrivate befriends Foo, and creates

Error deleting std::vector in a DLL using the PIMPL idiom

允我心安 提交于 2019-12-02 22:49:26
问题 I have the following code: In DLL1: in .h file: class MyClass { public: MyClass(); private: std::string m_name; }; class __declspec(dllexport) Foo { private: struct Impl; Impl *pimpl; public: Foo(); virtual ~Foo(); }; struct Foo::Impl { std::vector<MyClass> m_vec; std::vector<MyClass> &GetVector() { return m_vec; }; }; in .cpp file: Foo::Foo() : pimpl ( new Impl ) { } Foo::~Foo() { delete pimpl; pimpl = NULL; } [EDIT] In DLL2 in .h class Bar : public Foo { public: Bar(); virtual ~Bar(); }; in

Error deleting std::vector in a DLL using the PIMPL idiom

ぃ、小莉子 提交于 2019-12-02 13:07:08
I have the following code: In DLL1: in .h file: class MyClass { public: MyClass(); private: std::string m_name; }; class __declspec(dllexport) Foo { private: struct Impl; Impl *pimpl; public: Foo(); virtual ~Foo(); }; struct Foo::Impl { std::vector<MyClass> m_vec; std::vector<MyClass> &GetVector() { return m_vec; }; }; in .cpp file: Foo::Foo() : pimpl ( new Impl ) { } Foo::~Foo() { delete pimpl; pimpl = NULL; } [EDIT] In DLL2 in .h class Bar : public Foo { public: Bar(); virtual ~Bar(); }; in .cpp: Bar::Bar() { } Bar::~Bar() { } In DLL3: extern "C" __declspec(dllexport) Foo *MyFunc(Foo *param)

C++: Creating a shared object rather than a shared pointer to an object

我与影子孤独终老i 提交于 2019-12-01 19:30:00
boost::shared_ptr really bothers me. Certainly, I understand the utility of such a thing, but I wish that I could use the shared_ptr<A> as an A* . Consider the following code class A { public: A() {} A(int x) {mX = x;} virtual void setX(int x) {mX = x;} virtual int getX() const {return mX;} private: int mX; }; class HelpfulContainer { public: //Don't worry, I'll manager the memory from here. void eventHorizon(A*& a) { cout << "It's too late to save it now!" << endl; delete a; a = NULL; } }; int main() { HelpfulContainer helpfulContainer; A* a1 = new A(1); A* a2 = new A(*a1); cout << "*a1 = " <

c++ pimpl idiom : Implementation depending on a template parameter

荒凉一梦 提交于 2019-12-01 15:10:50
In this question I unsuccessfully asked how to use different pimpl implementation depending on a template argument. Maybe this example ilustrates better what I am trying to do : #include <iostream> template< int N, typename T > struct B { B() : c( new C< N > ) {} template< int M > struct C; C< N > *c; }; template< int N, typename T > template< int M > struct B< N, T >::C { int a[M]; }; // version 1 that doesn't work template< int N, typename T > template< > struct B< N, T >::C< 0 > { int a; }; // version 2 that doesn't work template< typename T > template< int M > struct B< 0, T >::C { int a;

c++ pimpl idiom : Implementation depending on a template parameter

不羁的心 提交于 2019-12-01 13:24:45
问题 In this question I unsuccessfully asked how to use different pimpl implementation depending on a template argument. Maybe this example ilustrates better what I am trying to do : #include <iostream> template< int N, typename T > struct B { B() : c( new C< N > ) {} template< int M > struct C; C< N > *c; }; template< int N, typename T > template< int M > struct B< N, T >::C { int a[M]; }; // version 1 that doesn't work template< int N, typename T > template< > struct B< N, T >::C< 0 > { int a; }

Pimpl with unique_ptr : Why do I have to move definition of constructor of interface to “.cpp”?

限于喜欢 提交于 2019-12-01 02:52:52
问题 The code would work file as long as I don't move the definition of constructor (of B ) to the header B.h . B.h class Imp; //<--- error here class B{ public: std::unique_ptr<Imp> imp; B(); //<--- move definition to here will compile error ~B(); //// .... other functions .... }; B.cpp #include "B.h" #include "Imp.h" B::B(){ } ~B::B(){ } Imp.h class Imp{}; Main.cpp (compile me) #include "B.h" Error: deletion of pointer to incomplete type Error: use of undefined type 'Imp' C2027 I can somehow