I l@ve RuBoard Previous Section Next Section

Item 28. Minimizing Compile-time Dependencies桺art 3

Difficulty: 7

Now the unnecessary headers have been removed, and needless dependencies on the internals of the class have been eliminated. Is there any further decoupling that can be done? The answer takes us back to basic principles of solid class design.

The Incredible Shrinking Header has now been greatly trimmed, but there may still be ways to reduce the dependencies further. What further #includes could be removed if we made further changes to X, and how?

This time, you may make any changes to X as long as they don't change its public interface so that existing code that uses X is unaffected beyond requiring a simple recompilation. Again, note that the comments are important.



//  x.h: after converting to use a Pimpl 


//       to hide implementation details


//





#include <iosfwd>


#include "a.h"  // class A (has virtual functions)


#include "b.h"  // class B (has no virtual functions)


class C;


class E;





class X : public A, private B


{


public:


     X( const C& );


  B  f( int, char* );


  C  f( int, C );


  C& g( B );


  E  h( E );


  virtual std::ostream& print( std::ostream& ) const;


private:


  struct XImpl;


  XImpl* pimpl_;


    // opaque pointer to forward-declared class


};





inline std::ostream& operator<<( std::ostream& os, const X& x )


{


  return x.print(os);


}


    I l@ve RuBoard Previous Section Next Section