父类包含子类C ++的静态实例

时间:2015-01-04 13:51:51

标签: c++ xcode

我声明了两个类,如下所示,A是父类,B子类A:

//a.h
#include "b.h"
//class B;     Adding this line doesn't work
class A{
    static B b;
}

//b.h
#include "a.h"
class B:public A{     // XCode error here: expected class name

}

但是,XCode 6.1不允许我编译并继续说“期望的类名”。

事实上,我正在尝试实现游戏编程模式http://gameprogrammingpatterns.com/state.html#static-states一书中提到的状态机。在该书中,父状态类包含子类的静态实例。

1 个答案:

答案 0 :(得分:3)

下面的代码就足够了: -

//b.h
#include "a.h"     <<<< This requires full definition for `A`.
class B : public A
{ 

}

//a.h         <<<<< No need to include any file.
class B;
class A{
    static B b;
};