C ++中的循环依赖辅助

时间:2011-12-10 01:43:57

标签: c++ include visual-c++-2010-express

我有一个类似于下面的代码,但我不知道如何让它工作。

我已经搜索了它并且看起来是关于循环依赖的东西,但是现在,我已经尝试了一些示例,但只能使用2的依赖项。

相反,这个,我有许多类依赖它们的“Ctrl”类(CtrlA和CtrlB是依赖的,Ax类需要两个Ctrl),但是其中一些clases也需要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

A1.h

#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

A2.h

#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;
}

如果有人能帮助我纠正代码以便它可以正常工作,我将非常感激。

2 个答案:

答案 0 :(得分:1)

对于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

<强> A1.h

#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

<强> A2.h

#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,你想要包括'em all:

<强>的main.cpp

#include "CtrlA.h"
#include "CtrlB.h"
#include "A1.h"
#include "A2.h"
#include "A3.h"

int main(){
    int i;
}

希望有所帮助! Here is a quick reference转发声明以及何时/如何使用它。

编辑:感谢Pablo指出我的错误。您不能将前向声明的类用作成员对象,只能使用引用或指针。我已经将上面的例子改为使用指针。

答案 1 :(得分:0)

我的建议:使用更多指针和 尽可能多的前向声明 (并避免#include .h文件中的其他标题。#include它们在.cpp文件中需要实施的地方。)

所以

CtrlA.h

#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

You cannot declare a incomplete type as an member.

您将在this question

的第二个回答中找到一些有用的信息