40 likes | 79 Views
PIMPL Idiom. Encapsulating Implementation. Nested Classes. nested class is a class declared inside another class inner class scope is the enclosing class scope limitation is the primary purpose for this construct
E N D
PIMPL Idiom Encapsulating Implementation
Nested Classes • nested class is a class declared inside another class • inner class • scope is the enclosing class • scope limitation is the primary purpose for this construct • if object or method is mentioned outside of enclosing class, need to resolve the scope • can be private or public • can be forward declared and then defined outside • if forward declared, the forward declaration should be inside the enclosing class definition (elaborated type specifier does not work) • has access to private/public members of enclosing class • enclosing class has no access to private/public members of inner class
PIMPL • C++ does not provide ways to hide class implementation details/data: private part has to be declared • clients include header file with class definition: exposed to implementation • pointer to implementation (PIMPL) idiom: aka opaque pointer, d-pointer, cheshire cat idiom • in private (implementation) section of class provide reference (pointer) to actual implementation • when interface methods are invoked, invoke corresponding implementation methods using the pointer • actual implementation class is defined and implemented in separately
PIMPL (cont) • terms: • handle – class with exposed interface • body – implementation class • specifics • body needs to be forward declared • body is dynamically allocated – need to implement big three • good practice to nest body inside handle • advantages • encapsulates implementation, allows body modification without need to modify handle • speeds up compilation, provides binary compatibility • could be used in Memento and Bridge design pattern