Improved some of the pImpl code.
David Blume

David Blume commited on 2016-09-27 16:49:53
Showing 3 changed files, with 18 additions and 7 deletions.

... ...
@@ -112,9 +112,8 @@ int main() {
112 112
 	}
113 113
 
114 114
     {   // Testing Herb Sutter's pImpl template
115
-        // Fails with static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");
116
-        //std::unique_ptr<MyClass> my_class;
117
-        //my_class->f();
115
+        std::unique_ptr<MyClass> my_class(std::make_unique<MyClass>());
116
+        my_class->f();
118 117
     }
119 118
 
120 119
     // This_function_is_not_defined();
... ...
@@ -20,13 +20,22 @@ int main_helper_fn() {
20 20
 }
21 21
 
22 22
 
23
-class MyClass::Impl {
23
+class MyClass::Impl 
24
+{
24 25
 public:
25 26
     void f() {
26 27
         cout << "The function " << __PRETTY_FUNCTION__ << " was called." << endl;
27 28
     };
28 29
 };
29 30
 
31
+MyClass::MyClass()
32
+  : pimpl{std::make_unique<Impl>()} 
33
+{
34
+}
35
+
36
+// See http://www.cppsamples.com/common-tasks/pimpl.html
37
+MyClass::~MyClass() = default;
38
+
30 39
 void MyClass::f()
31 40
 {
32 41
     pimpl->f();
... ...
@@ -11,12 +11,15 @@ template<class T>
11 11
 using Pimpl = const std::unique_ptr<T>;
12 12
 
13 13
 class MyClass {
14
+public:
15
+    MyClass();
16
+    ~MyClass();
17
+ 
18
+    void f();
14 19
 
20
+ private:
15 21
     class Impl;  // defined in cpp
16 22
     Pimpl<Impl> pimpl;
17
-
18
-public:
19
-    void f();
20 23
 };
21 24
 
22 25
 
23 26