mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Private constructor does not allow to create derived classes.
This commit is contained in:
parent
bf8f1b8f49
commit
240bc0ea0c
|
@ -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<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.
|
||||
|
||||
**Note**: Conventional factory functions allocate on the free store, rather than on the stack or in an enclosing object.
|
||||
|
|
Loading…
Reference in New Issue
Block a user