[英]Parent class holds a static instance of child class C++
I am declaring two classes like the following, A is the parent class, B subclass A: 我在声明两个类似以下的类,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
}
However, XCode 6.1 don't let me compile and keeps saying "expected class name". 但是,XCode 6.1不允许我编译,并一直说“预期的类名”。
In fact, I am trying to implement a state machine mentioned in the book Game Programming Patterns http://gameprogrammingpatterns.com/state.html#static-states . 实际上,我正在尝试实现《游戏编程模式》一书( http://gameprogrammingpatterns.com/state.html#static-states)中提到的状态机。 In that book, the parent state class holds static instances of the child classes. 在那本书中,父状态类包含子类的静态实例。
Below code would suffice for you :- 下面的代码将满足您的需求:
//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;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.