From 240bc0ea0c501fb34b057cf0eff3240f9ca491f2 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Sat, 26 Sep 2015 13:59:26 +0300 Subject: [PATCH 1/2] Private constructor does not allow to create derived classes. --- CppCoreGuidelines.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 380bfbf..da75125 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3833,7 +3833,7 @@ How would a maintainer know whether `j` was deliberately uninitialized (probably **Example*: class B { - private: + protected: B() { /* ... */ } // create an imperfectly initialized object virtual void PostInitialize() // to be called right after construction @@ -3859,7 +3859,7 @@ How would a maintainer know whether `j` was deliberately uninitialized (probably shared_ptr p = D::Create(); // creating a D object -By making the constructor `private` we avoid an incompletely constructed object escaping into the wild. +By making the constructor `protected` we avoid an incompletely constructed object escaping into the wild. By providing the factory function `Create()`, we make construction (on the free store) convenient. **Note**: Conventional factory functions allocate on the free store, rather than on the stack or in an enclosing object. From 9222fd1b1f505fcce7ce932b8b85520b86691c44 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Sat, 26 Sep 2015 14:00:52 +0300 Subject: [PATCH 2/2] Fix typo. --- CppCoreGuidelines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index da75125..c4f2e3c 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3532,7 +3532,7 @@ The idiom of having constructors acquire resources and destructors release them class X3 { // bad: the constructor leaves a non-valid object behind FILE* f; // call init() before any other function - bool valid;; + bool valid; // ... public: X3(const string& name)