复杂循环依赖 [英] Complex circular dependency

查看:135
本文介绍了复杂循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++



中解决循环依赖的最佳实践是什么? ,但是我得到指向不完整类类型的指针是不允许的错误。这是否意味着使用每个其他指针的两个类不能依赖?



此外,我想到了forward声明每个类,然后包括解决方案的每个头, code> main.cpp ,所以它都在一个地方。你会推荐它吗?



整个项目的代码段在下面,所以如果问题更好地解释一个我熟悉的例子,但它只是理论上的。
感谢



解决方案

您只需要正确使用转发声明:


  1. 将所有代码放在cpp文件中

  2. 在头文件中放入类声明

  3. 在头文件中:


    1. 如果只使用指针或引用,请使用forward声明。

    2. 否则,您需要包含头文件。 (不要添加不需要的内容)


  4. 在cpp文件中

      $ b $



      它实际上很难做到没有实际的声明。图表是好的,但没有足够的信息。图片可能值一千字,但精确定义的语言可以非常紧凑地传达更准确的信息(不像英语及其不一致)。


      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 class type is not allowed error. Does that mean that two classes that uses each others pointer cannot be dependent?

      Also, I thought about forward declaring each class and then including every header of the solution in the main.cpp, so it's all in one place. Would you recommend it?

      A snippet from the whole project is below, so you can refer to it if the issue is better explained on an example I'm familiar with, but it would do just to be theoretical. Thanks

      解决方案

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

      这篇关于复杂循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆