Pimpl mode of C + +

Source: Internet
Author: User

1 pimpl explanation

Pimpl (Private implementation or Pointer to implementation) is a private member pointer that hides the internal implementation data of the class to which the pointer points.

2 Pimpl Advantages

Example:

X.hclass x{public:    void Fun ();p rivate:    int i;//add int i;};/ /c.h#include <x.h>class c{public:    void Fun ();p rivate:    x x;//strong coupling to X}; Pimpl procedure://c.hclass X; Instead of # include <x.h>class c{public:    void Fun ();p rivate:    x *pimpl;//pimpl};

1) Reduce the coupling of the module. because the implementation of the class is hidden, the hidden class is the same as the original class is invisible, the hidden class is modified, and the original class does not need to be recompiled.

2) Reduce compilation dependencies and improve compilation speed. the size of the pointer is (32-bit) or 8 (64-bit), the x changes, the pointer size does not change, and the file c.h does not need to be recompiled.

3) interface and implementation separation, improve the stability of the interface.

1, through the pointer encapsulation, when the definition of "new C" or "C C1", the compiler generated code will not be doped with any information of X.

2, when using C, the use of C interface (c interface operation of the class is actually the Pimpl member pointed to the X object), and x Independent, x is through the pointer encapsulation thorough and implementation separation.

C.cppc::c () Pimpl (New X ()) {}c::~c () {     delete pimpl;     Pimpl = NULL;} void C::fun () {    pimpl->fun ();} Main#include <c.h>int Main () {    C C1;    C1. Fun ();    return 0;}

Instance code:

Nestcalss.h

#ifndef __line_h__#define __line_h__//design mode: PIMPL//1. Implement information hiding//2. Reduced compilation dependency, can be smoothed with minimal cost to upgrade the library file,//3. interface and implementation are decoupled class Line{public:line (Int,int,int,int); ~line (); void PrintLine () const;private:class lineimpl;private: Lineimpl * _PIMPL;}; #endif

  

nestclass.cc

#include "nestClass.h" #include <math.h> #include <iostream>using std::cout;using std::endl;class line:: Lineimpl{class point{public:point (int ix = 0, int iy = 0): _ix (ix), _iy (iy) {cout << "point (int=0, int=0)" << E NDL;} void print () const{cout << "(" << _ix << "," << _iy << ")";} Private:int _ix;int _iy;}; Public:lineimpl (int x1, int y1, int x2, int y2): _p1 (x1, y1), _p2 (x2, y2) {cout << Lineimpl (int,int,int,int) &LT;&L T Endl;} ~lineimpl () {cout << "~lineimpl ()" << Endl;} void PrintLine () const;private:point _p1; Point _p2;}; void Line::lineimpl::p rintline () const{_p1.print () cout << "--"; _p2.print (); cout << Endl;} Line::line (int x1, int y1, int x2, int y2): _pimpl (New Lineimpl (x1, y1, x2, y2)) {cout << line (int,int,int,int) &lt ;< Endl;} Line::~line () {delete _pimpl;cout << "~line ()" << Endl;} void line::p rintline () Const{_pimpl->printline ();}

  

main.cc

#include "nestClass.h" #include <iostream>using std::cout;using Std::endl; int main (void) {line line (1, 2, 3, 4); Line.printline (); return 0;}

  

Pimpl mode of C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.