C ++ creates a header to solve circular dependency - is it a good idea?

I have a C ++ project that is starting to get big. I came across a situation where I have many classes with circular dependencies. Suppose I have classes A, B, C, D. To solve this problem, I created a header file called circle_dep.h

The contents of this file will be a pointer to the classes:

class A; class B; class C; class D; 

This title is now included with the entire title of my classes. So the compiler stopped complaining when I had circular dependencies. However, when I add a new class to the project, I will have to add it to round_dep.h

I could not find an easier way to do this, so for me this is the best solution. My question is: is it right to apply projects that may run into problems of circular dependence? Or is it a bad design / bad or dangerous practice?

+5
source share
1 answer

Are you all right. The naming suffix that I usually saw is _fwd.h / _fwd.hpp / _fwd.hxx or any other .h variant that 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 grammar, so you have no choice.)

+2
source

Source: https://habr.com/ru/post/1246597/


All Articles