I have a C++ project that's starting to get big. I'm facing a situation where I have lots of classes with circular dependencies. Suppose I have classes A, B, C, D. To solve this, I've created a header file named circular_dep.h
This file's content would be pointers to classes:
class A;
class B;
class C;
class D;
This header is included by all my classes's header now. That way, the compiler stopped complaining when I had circular dependencies. Whenever I add a new class to the project, however, I'll have to add it to circular_dep.h
I couldn't find any easier way to do this, so to me this is the best solution so far. My question is: is it a good idea to apply to projects that might face circular dependency issues? Or is this a bad design/bad or dangerous practice?
What you have is fine. The naming suffix I've usually seen is _fwd.h
/_fwd.hpp
/_fwd.hxx
or whatever variant of .h
you like. You can see this in Boost, for example: optional_fwd.hpp.
(Of course reducing circular dependencies is a good goal, but some things are mutually-recursive in nature, like grammars, so you have no choice.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With