pImpl
pImpl Idiom in C++
Loading...
Searching...
No Matches
PimplClass

PimplClass. More...

#include <pImpl.h>

Collaboration diagram for PimplClass:

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< Privated_ptr
 The private class pointer.
 

Detailed Description

PimplClass.

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.

Definition at line 39 of file pImpl.h.

Constructor & Destructor Documentation

◆ ~PimplClass()

virtual PimplClass::~PimplClass ( )
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.

◆ PimplClass()

PimplClass::PimplClass ( Private dd)
protected

Construct a new PimplClass object.

This constructor must be used by subclasses to initialize the private class.

Parameters
ddreference to the private class

Member Function Documentation

◆ clear()

virtual void PimplClass::clear ( )
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).

◆ isNull()

bool PimplClass::isNull ( ) const

Check if the private class is null.

This can happen when moving the object with std::move.

Returns
true if the private class is null

Member Data Documentation

◆ d_ptr

std::unique_ptr<Private> PimplClass::d_ptr
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.

Definition at line 94 of file pImpl.h.