[英]Circular dependency assistance in C++
我有一個類似於以下代碼的代碼,但是我無法獲得如何使其工作的代碼。
我已經搜索過它,看起來與循環依賴有關,但是現在,我嘗試了一些示例,但僅在2的依賴下工作。
相反,我有一個“ Ctrl”類,其中許多類都依賴於它們(CtrlA和CtrlB相互依賴,Ax類都需要Ctrl),但是其中某些類也需要Ctrl文件(CtrlA需要Axe類)。 另外,我有一個繼承的類(A2繼承了A3)。
CtrlA.h
#ifndef CTRLA
#define CTRLA
#include "CtrlB.h"
#include "A1.h"
class CtrlB;
class A1;
class CtrlA{
protected:
A1 x;
public:
void op1(CtrlB b){
a.op1(this, b);
}
void op2(){}
};
#endif
CtrlB.h
#ifndef CTRLB
#define CTRLB
#include "CtrlA.h"
class CtrlA;
class CtrlB{
protected:
public:
void op1(){}
void op2(CtrlA a){
a.op1(this);
}
};
#endif
1小時
#ifndef A1
#define A1
#include "CtrlA.h"
#include "CtrlB.h"
#include "A2.h"
class CtrlA;
class CtrlB;
class A1{
protected:
A2 x1;
public:
void op1(CtrlA a, CtrlB b){
x1.op1(this, b);
}
};
#endif
2小時
#ifndef A2
#define A2
#include "CtrlA.h"
#include "CtrlB.h"
#include "A3.h"
class CtrlA;
class CtrlB;
class A2:public A3{
protected:
public:
void op1(CtrlA a, CtrlB b){
a.op2();
b.op1();
}
};
#endif
A3.h
#ifndef A3
#define A3
#include "CtrlA.h"
#include "CtrlB.h"
class CtrlA;
class CtrlB;
class A3{
protected:
public:
virtual void op1(CtrlA a, CtrlB b) = 0;
};
#endif
main.cpp
#include "CtrlA.h"
#include "CtrlB.h"
int main(){
int i;
}
如果有人可以幫助我更正代碼以使其起作用,我將不勝感激。
對於CtrlA.h,CtrlB.h,A1.h和A3.h,如果使用前向聲明(已進行排序)並使用引用或指針(未進行過),則無需#include任何內容:
CtrlA.h
#ifndef CTRLA
#define CTRLA
class CtrlB;
class A1;
class CtrlA {
protected:
A1* x;
public:
/* Use a CtrlB reference instead -- probably wanted to do this anyway
/* since you don't want to copy CtrlB when called */
void op1(CtrlB& b); /* Move function body to .cpp file */
void op2(){}
};
#endif
1小時
#ifndef A1
#define A1
class CtrlA;
class CtrlB;
class A2; /* You have to use forward declaration on every class you use below */
class A1{
protected:
A2* x1;
public:
void op1(CtrlA& a, CtrlB& b); /* Again, use references and move function
bodies to .cpp */
};
#endif
但是使用A2.h,您將從A3繼承,因此您必須#include A3.h
2小時
#ifndef A2
#define A2
#include "A3.h"
class CtrlA;
class CtrlB;
class A2:public A3{
protected:
public:
void op1(CtrlA& a, CtrlB& b);
};
#endif
剩下main.cpp,您將在其中包括所有內容:
main.cpp
#include "CtrlA.h"
#include "CtrlB.h"
#include "A1.h"
#include "A2.h"
#include "A3.h"
int main(){
int i;
}
編輯:感謝Pablo指出了我的錯誤。 您不能將前向聲明的類用作成員對象,只能將其用作引用或指針。 我已經更改了上面的示例以使用指針。
我的建議:使用更多的指針和盡可能多的前向聲明 (並避免#包括.h文件中的其他標頭。#將它們包括在需要實現的.cpp文件中。)
所以
#ifndef CTRLA
#define CTRLA
//#include "CtrlB.h"//no, don't #include this here, use fwd decl below
//#include "A1.h" //no
class CtrlB; // yes, use forward decl ONLY as much as possible
class A1; // yes
// the only reason you'd NEED to have an #include is
// if CtrlA INHERITED CtrlB. Then you'd need to #include "CtrlB.h"
class CtrlA{
protected:
A1 *x; // make PTR, meaning you can live off
// the fwd declaration in the header file
// (but will need to #include "A1.h" in
// the .cpp file to use member functions of A1)
public:
void op1(CtrlB *b) ; // CtrlB ptr, NO IMPLEMENTATION HERE, put in .cpp
void op2() ; // no impl
};
#endif
您會在此問題的第二個答案中找到一些有用的信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.