Nat*_*air 2 c++ inheritance class
我试图理解是否有一种方法为基类设置include指令,但也能够访问在与基类不同的头中定义的派生类.例如:
在GenericObject.h中:
class GenericObject {
/* whatever - shared properties and methods implemented in .cpp file */
}
Run Code Online (Sandbox Code Playgroud)
在Ball.h:
class Ball : public GenericObject {
/* whatever - implementation details are in the .cpp file */
}
Run Code Online (Sandbox Code Playgroud)
现在,在一些其他的实现文件(让我们把它叫做main.cpp中),我#includeð "GenericObject.h".在main.cpp中,我希望能够访问GenericObject类的成员以及Ball类和GenericObject的任何其他派生类.从我到目前为止的试验来看,如果我尝试声明它Ball * b;并且因此属于它的类的任何成员,编译器将不会识别Ball标识符.
这是因为我的#include'd "GenericObject.h"文件还没有链接,我可以访问这个派生的Ball类吗?我可以正确地识别并使用Ball类,"#include "Ball.h"但我无法想象我应该包含GenericObject类的派生类的所有头文件,对吧?
如果我不得不猜测它是如何工作的,我猜想像GenericObject类中的Ball类的原型声明会朝着正确的方向迈出一步但是我不确定类原型是否是甚至是一件事.
感谢阅读和大家的任何反馈!