假设我们有这样的情况:声明为外部变量如基类和派生类指针
base.h:
class Base { };
derived.h:
#include "base.h"
class Derived : public Base { };
extern Derived *variable;
derived.cpp :
#include "derived.h"
Derived *variable;
是否正确t宣布variable
作为指向Base
在别处cpp?
class Base;
extern Base *variable;
C++ Builder链接器没有抱怨,一切似乎工作。根据标准,这是安全和正确的,还是每个variable
的声明都应该是同一类型的?