Thursday, June 21, 2007

Minimize compilation dependencies between files

In general a class specifies not just interface but a fair no of implementation details. Like private variables, inline functions etc.

Including standard headers is unlikely to be the problem as you can take advantage of precompiled headers.

You can use the pimpl idiom to get around with hiding the impl from the clients

I don't understand what is the meaning of replace dependencies on definitions with dependencies on declarations. ????
This minimizes compilation dependencies.

Two rules to follow:
Avoid using objects when object references and pointers will do
Depend on class declarations instead of class definitions whenever you can. ---I don't understand the note about moving dependencies to client's files at bottom of page 143 ???

Provide separate header files for declarations and definitions.

Look at iosfwd header file.???

Handle classes are classes that employ the pimpl idiom
They generally forward all their calls to the base class.

Or you can have an interface class exposed to client and actual class that implements the interface. Then you provide a static factory function that gives the actual class ( typically a shared pointer to it).

Obviously both of these approaches have the cost of most fn calls being virtual function calls.

Things to remember:
The general idea behind minimizing compilation dependencies is to depend on declarations instead of definitions. Two approaches based on this idea are handle classes and interface classes.

Library header files should exist in full and declaration only forms,. This applies regardless of whether templates are involved or not

No comments: