Download presentation
Presentation is loading. Please wait.
Published byColeen Sullivan Modified over 8 years ago
1
PIMPL Idiom Encapsulating Implementation
2
Forward Declaration Revisited forward class declaration – names the class, does not provide definition class MyClass; –allows compiler to do elementary type checking –simplifies header files forward class declaration specifies incomplete type: may only declare pointers and references elaborated type specifier: forward class combined with pointer/reference declaration –have to be mentioned every pointer/reference declaration class MyClass *p; class Myclass &r; 2
3
Nested Classes nested class is an class declared inside another class inner class –scope is the outer class scope limitation is the primary purpose for this construct –if object or method is mentioned outside of outer 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 outer class definition –has access to private/public members of outer class –outer class has no access to private/public members of inner class 3
4
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 4
5
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 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.