如何在 C++ 中声明 extern class 指针?
How to declare extern class pointers in C++?
下面是在cpp文件中声明的变量,但是报错,于是研究了一下,发现需要在头文件中声明。因此我如何在头文件
中声明和一个extern class指针
extern AnimationController* g_pAnimationController;
就像你在那里一样。例如:
// In header file:
// Declare the pointer with external linkage.
extern int* my_global_int;
// In source file:
// Define the pointer so that the linker can link stuff together with the code
// referencing the `my_global_int` symbol.
int* my_global_int = 0;
对于类和结构,如果类型未知,那么我们需要一个前向声明,以便编译器知道它是什么。但我们可以将它与声明结合起来,像这样:
// In header file:
extern class AnimationController* g_pAnimationController;
或者写得更冗长:
// In header file:
class AnimationController;
extern AnimationController* g_pAnimationController;
评论问题更新:
#include <map>
#include <string>
// Declare AnimationCallback
extern std::map<std::string, AnimationCallback*> g_mCallbackMap;
下面是在cpp文件中声明的变量,但是报错,于是研究了一下,发现需要在头文件中声明。因此我如何在头文件
中声明和一个extern class指针 extern AnimationController* g_pAnimationController;
就像你在那里一样。例如:
// In header file:
// Declare the pointer with external linkage.
extern int* my_global_int;
// In source file:
// Define the pointer so that the linker can link stuff together with the code
// referencing the `my_global_int` symbol.
int* my_global_int = 0;
对于类和结构,如果类型未知,那么我们需要一个前向声明,以便编译器知道它是什么。但我们可以将它与声明结合起来,像这样:
// In header file:
extern class AnimationController* g_pAnimationController;
或者写得更冗长:
// In header file:
class AnimationController;
extern AnimationController* g_pAnimationController;
评论问题更新:
#include <map>
#include <string>
// Declare AnimationCallback
extern std::map<std::string, AnimationCallback*> g_mCallbackMap;