pImpl
pImpl Idiom in C++
|
#include <pImpl.h>
Classes | |
struct | Private |
The PimplClass::Private class. More... | |
Public Member Functions | |
virtual | ~PimplClass () |
Destroy the PimplClass object. | |
bool | isNull () const |
Check if the private class is null. | |
virtual void | clear () |
Clear the private class. | |
Protected Member Functions | |
PimplClass (Private &dd) | |
Construct a new PimplClass object. | |
Protected Attributes | |
std::unique_ptr< Private > | d_ptr |
The private class pointer. | |
PimplClass is the base class for the pImpl idiom.
The pImpl idiom is used to hide the private implementation of a class from clients. It also enables binary compatibility between releases.
There is no public constructor, only subclasses may be instantiated. This prevents the instantiation of the base class.
|
virtual |
Destroy the PimplClass object.
Using a unique_ptr that points to a private class that is not fully defined requires not using the default destructor implementation. Must be virtual to allow subclasses to be deleted through a pointer to the base class.
|
protected |
Construct a new PimplClass object.
This constructor must be used by subclasses to initialize the private class.
dd | reference to the private class |
|
virtual |
Clear the private class.
This can be used to reset the private class to a default state (same as after default construction) The default implementation does nothing, but subclasses may override it to clear or reset their private class (reset if isNull() returns true).
bool PimplClass::isNull | ( | ) | const |
Check if the private class is null.
This can happen when moving the object with std::move.
|
protected |
The private class pointer.
The private class pointer is a unique_ptr that points to the private class. unique_ptr is used to ensure that the private class is deleted when the API class is deleted. Thus the private class is deleted when the last reference to the API class is deleted, no matter how it is deleted.