mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Merge pull request #151 from egorpugin/master
Private constructor does not allow to create derived classes
This commit is contained in:
@@ -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
|
class X3 { // bad: the constructor leaves a non-valid object behind
|
||||||
FILE* f; // call init() before any other function
|
FILE* f; // call init() before any other function
|
||||||
bool valid;;
|
bool valid;
|
||||||
// ...
|
// ...
|
||||||
public:
|
public:
|
||||||
X3(const string& name)
|
X3(const string& name)
|
||||||
@@ -3833,7 +3833,7 @@ How would a maintainer know whether `j` was deliberately uninitialized (probably
|
|||||||
**Example*:
|
**Example*:
|
||||||
|
|
||||||
class B {
|
class B {
|
||||||
private:
|
protected:
|
||||||
B() { /* ... */ } // create an imperfectly initialized object
|
B() { /* ... */ } // create an imperfectly initialized object
|
||||||
|
|
||||||
virtual void PostInitialize() // to be called right after construction
|
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<D> p = D::Create<D>(); // creating a D object
|
shared_ptr<D> p = D::Create<D>(); // 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.
|
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.
|
**Note**: Conventional factory functions allocate on the free store, rather than on the stack or in an enclosing object.
|
||||||
|
|||||||
Reference in New Issue
Block a user