简体   繁体   English

这是循环依赖吗

[英]Is this a circular dependency

//A.h  
class B;  
class A{  
  void Stuff();
  B* FOO():  
  B* _b;
}  
extern A* A_A();

//A.cpp  
#include "A.h"  
#include "B.h" 
B* A::FOO(){
  return(_b);
} 

//B.h
class B{
 void BOO();
}

//B.cpp
#include "A.h"
#include "B.h" 
void B::BOO(){
 A_A->Stuff();
}

Here there is a cross include of the .h files from the .cpp files. 这是.cpp文件中.h文件的交叉包含。 So they both depend on one another. 因此,他们彼此依赖。 Though using the forward declaration and pointers it seems like that would break the cycle. 尽管使用前向声明和指针似乎会中断循环。 So my question is: Is this a circular dependency? 所以我的问题是:这是循环依赖吗? Why? 为什么?

When A depends on B and vice versa, you have a circular dependency, by definition. A依赖于B ,反之亦然,根据定义,您具有循环依赖关系。 The fact that you can get it to work with a forward declaration doesn't change that fact. 您可以使它与前向声明一起使用的事实并不会改变这一事实。

You've broken the circular include chain with the forward declarations, but you still have a logical circular dependency between A and B. They each require things provided by the other class. 您已经用正向声明打破了循环包含链,但是在A和B之间仍然存在逻辑循环依赖关系。它们各自需要其他类提供的东西。

Regarding your comment above, you should never feel ashamed to bring questions to your team. 关于上面的评论,您永远不要为向团队提出问题而感到羞耻。 If find out you were wrong, then you've learned something. 如果发现您错了,那么您已经学到了一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM