Complex circular dependency

后端 未结 3 901
再見小時候
再見小時候 2020-12-18 04:53

what is the the best practice of solving circular dependency in C++?

I could use the forward declaration, but then I get the pointer to incomplete

3条回答
  •  -上瘾入骨i
    2020-12-18 05:15

    You just need to use forward declaration correctly:

    1. Put all code in cpp files
    2. Put just class declaration in header file
    3. In header file:
      1. Use forward declaration if you only use a pointer or a reference.
      2. Otherwise you to include header file. (Do Not add unrequired includes)
    4. In cpp file
      1. include all header files you require.

    Note: Add include guards.

    Its hard to actually do it without the actual declarations. The diagram is nice but does not have enough information. A picture may be worth a thousand words, but a precisely defined language can convey more exact information very compactly (unlike English and its inconsistencies).

提交回复
热议问题