PIMPL Idiom Encapsulating Implementation Nested Classes nested class

  • Slides: 4
Download presentation
PIMPL Idiom Encapsulating Implementation

PIMPL Idiom Encapsulating Implementation

Nested Classes • • nested class is a class declared inside another class inner

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 members of inner class 2

PIMPL • • C++ does not provide ways to hide class implementation details/data: private

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 3

PIMPL (cont) • • terms: – handle – class with exposed interface – body

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 4