Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C++

Use of PIMPL Design Pattern

Rate me:
Please Sign up or sign in to vote.
2.77/5 (16 votes)
6 Feb 2007CPOL1 min read 86.5K   20   15
Explain why and how to use PIMPL

Introduction

PIMPL (Private Implementation) is a simple way to hide a part of a class implementation to other classes of the same project.

It makes it possible to avoid other classes to know internal data structures and other information of the class. It also simplifies some #include preprocessor instructions.

Example WITHOUT PIMPL

We have a CFoo class which is defined in two files foo.h and foo.cpp:

Our main instantiates this class to run one of its functions.

File foo.h

C++
class CFoo
{
public:
    CFoo();
    ~CFoo();
    bool ProcessFile(const CString & csFile);
private:
    CFooInternalData    m_data;
    CHeader             m_header;
} 

File foo.cpp

C++
#include "FooInternalData.h"
#include "Header.h"
#include "foo.h"
CFoo::CFoo()
{
}

CFoo::~CFoo()
{
}

bool CFoo::ProcessFile(const CString & csFile)
{
    //do something
    return true;
}

Main File

C++
#include "FooInternalData.h"
#include "Header.h"
#include "foo.h"

int main()
{
    CFoo foo;
    foo.ProcessFile("c:\\data.bin");
    return 0;
} 

The problem with this simple way of coding is that in your main file, you must include the foo.h to use it, but at the same time you must also include all needed files to allow the compiler to work correctly. In fact, the main does not need to include FooInternalData.h and Header.h (which are CFoo internal structures) except for compilation.... So with very big classes, you might do some huge includes and in this case, you can have some compiler or linker errors because files are already included elsewhere.

The Same Example with PIMPL

File foo.h

C++
//here just declare the class PIMPL to compile. 
//As I use this class with a pointer, I can use this declaration 
class CFoo_pimpl; 

class CFoo
{
public:
    CFoo();
    ~CFoo();
    bool ProcessFile(const CString & csFile);
private:
    std::auto_ptr<CFoo_pimpl>    m_pImpl;
}  

File foo.cpp

C++
#include "FooInternalData.h"
#include "Header.h"
#include "foo.h"

//here defines PIMPl class, because it is use only in this file
class CFoo_pimpl()
{
public:
    CFoo_pimpl()
    {
    }

    ~CFoo_pimpl()
    {
    }  
    bool ProcessFile(const CString & csFile)
    {
        //do something
        return true;
    }
};

CFoo::CFoo()
:m_pImpl(new CFoo_pimpl())
{
}

CFoo::~CFoo()
{
    //do not have to delete, std::auto_ptr is very nice 
}

bool CFoo::ProcessFile(const CString & csFile)
{
    //just call your PIMPL function ;-)
    return m_pImpl->ProcessFile(csFile);
}

Main File

C++
#include "foo.h"
int main() 
{
    CFoo foo;
    foo.ProcessFile("c:\\data.bin");
    return 0; 
} 

The result is obvious: simplicity of use!! The main does not need more includes for internal structures of CFoo class.

Thus it is an excellent optimization to minimize linker and compiler errors.

Conclusion

It is a very simple and nice way for good coding!!! If you want to use classes in other projects, it does not introduce including difficulties.

Unfortunately, you must add some more code to type.

History

  • 6th February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer ECT Industries
France France
I am software engineer and I work for the aviation.

I'm currently working on many different project and in many different languages
- Visual C++ 6
- C#
- ASP.NET
- C and assembly

Have lot of fun

Comments and Discussions

 
GeneralMy vote of 1 Pin
quinquin17-Jul-12 22:50
quinquin17-Jul-12 22:50 
GeneralMy vote of 3 Pin
S p k 5212-Aug-11 18:59
S p k 5212-Aug-11 18:59 
GeneralMy vote of 1 Pin
chief editor22-Mar-11 11:26
chief editor22-Mar-11 11:26 
QuestionWhat happened Pin
garyco3-May-10 21:11
garyco3-May-10 21:11 
AnswerRe: What happened Pin
Le Sourcier3-May-10 21:45
Le Sourcier3-May-10 21:45 
QuestionWrapper Class? Pin
Prakash Nadar16-Jul-09 11:55
Prakash Nadar16-Jul-09 11:55 
GeneralMy vote of 1 Pin
_darksake_29-Jan-09 7:17
_darksake_29-Jan-09 7:17 
GeneralRe: My vote of 1 Pin
xComaWhitex29-Nov-09 15:56
xComaWhitex29-Nov-09 15:56 
Generalan other solution ... Pin
a.buisson12-Feb-07 20:18
a.buisson12-Feb-07 20:18 
I found your solution to hide the internal defintion of CFoo, very complex.
In fact, i prefer write something like :

class CFooInternalData;
class CHeader;

class CFoo
{
public:
CFoo();
~CFoo();
bool ProcessFile(const CString & csFile);
private:
CFooInternalData* m_data;
CHeader* m_header;
}

and update CFoo default construtor and destructor. I understand than class CFoo_pimpl is more efficient technic if there lot's of private member to hide and auto_ptr lead to simple objet/memory management. But i lot's of case we can use pimpl pattern without write a new hidden class.

Alex

Rule #1: Best optimization of a function is achieved by not calling it
Prefer a better algo, cache if possible, or precompute, instead
Rule #2: Optimize on purpose
or: "are you sure you need an ASM version of XOpenDisplay() ??"
Rule #3: Don't optimize a loop that hasn't been used untouched for at
least one month
or: "premature optimization is the mother of all evil"
Rule #4: Know when to stop
there's a no-return point where you'll be the only one to know what the
code does... and the

Generalperformance implications Pin
Emilio Garavaglia8-Feb-07 1:47
Emilio Garavaglia8-Feb-07 1:47 
GeneralRe: performance implications Pin
René Greiner12-Feb-07 6:07
René Greiner12-Feb-07 6:07 
GeneralYou forgot ... Pin
Sceptic Mole6-Feb-07 7:51
Sceptic Mole6-Feb-07 7:51 
GeneralRe: You forgot ... Pin
Le Sourcier6-Feb-07 20:48
Le Sourcier6-Feb-07 20:48 
GeneralRe: You forgot ... Pin
psyclonist7-Feb-07 1:01
psyclonist7-Feb-07 1:01 
GeneralRe: You forgot ... Pin
thomasCAE19-Feb-07 22:00
thomasCAE19-Feb-07 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.