Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ is creating a header to solve circular dependency a good idea?

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?

like image 388
Aleksandrus Avatar asked Oct 30 '22 06:10

Aleksandrus


1 Answers

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.)

like image 66
GManNickG Avatar answered Nov 12 '22 22:11

GManNickG