I l@ve RuBoard Previous Section Next Section

Item 29. Compilation Firewalls

Difficulty: 6

Using the Pimpl Idiom can dramatically reduce code interdependencies and build times. But what should go into a pimpl_ object, and what is the safest way to use it?

In C++, when anything in a class definition changes (even private members), all users of that class must be recompiled. To reduce these dependencies, a common technique is to use an opaque pointer to hide some of the implementation details.



class X 


{


public:


  /* ... public members ... */


protected:


  /* ... protected members? ... */


private:


  /* ... private members? ... */


  struct XImpl;


  XImpl* pimpl_;         // opaque pointer to


                         // forward-declared class


};


The questions for you to answer are:

  1. What should go into XImpl? There are four common disciplines.

    • Put all private data (but not functions) into XImpl.

    • Put all private members into XImpl.

    • Put all private and protected members into XImpl.

    • Make XImpl entirely the class that X would have been, and write X as only the public interface made up entirely of simple forwarding functions (a handle/body variant).

      What are the advantages/drawbacks of each? How would you choose among them?

  2. Does XImpl require a pointer back to the X object?

    I l@ve RuBoard Previous Section Next Section