I l@ve RuBoard Previous Section Next Section

Item 27. Minimizing Compile-time Dependencies桺art 2

Difficulty: 6

Now that the unnecessary headers have been removed, it's time for Phase 2: How can you limit dependencies on the internals of a class?

Below is how the header from Item 26 looks after the initial clean-up pass. What further #includes could be removed if we made some suitable changes, and how?

This time, you may make changes to X as long as X's base classes and its public interface remain unchanged; any current code that already uses X should not be affected beyond requiring a simple recompilation.



//  x.h: sans gratuitous headers 


//


#include <iosfwd>


#include <list>


// None of A, B, C or D are templates.


// Only A and C have virtual functions.


#include "a.h"  // class A


#include "b.h"  // class B


#include "c.h"  // class C


#include "d.h"  // class D


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:


  std::list<C> clist_;


  D            d_;


};


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


{


  return x.print(os);


}


    I l@ve RuBoard Previous Section Next Section