Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

C++: Fixing Cyclic Dependencies

📅 2010-Jul-22 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, dependencies ⬩ 📚 Archive


Here are some guidelines from experience that I have found useful to handle or fix cyclic dependencies in C++ code. These can be used on new code or to polish ugly old code back to a shiny state.

Class Triangle extends from class Facet, so Triangle depends on Facet. Draw an arrow from Triangle to Facet.

* ```cpp

// Triangle.h class Triangle { Point p[3]; };

Class Triangle has a member of class Point, so Triangle depends on Point. Draw an arrow from Triangle to Point.

    * ```cpp
// Quad.h
class Quad
    Point p[4];
    bool checkForTriangle( const Triangle& ) const;
    Triangle getTriangle( int ) const;

Class Quad has a method which has a parameter of class Triangle. It also has a method which returns a Triangle. Neither of this implies that Quad depends on Triangle.

Cyclic dependencies can be extremely hairy. Hopefully, these guidelines will help you fix them. 😊

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧