简体   繁体   English

如何在C ++中声明extern类指针?

[英]How to declare extern class pointers in C++?

The following is declared variable in the cpp file but I get an error, so I have conducted a research and I found that I need to declare it in the header file. 以下内容在cpp文件中声明为变量,但出现错误,因此进行了研究,发现需要在头文件中声明它。 therefore how can I declare and an extern class pointer in the header file 因此,如何在头文件中声明和extern类指针

 extern AnimationController* g_pAnimationController;

Just like how you have there. 就像你在那里一样。 Ex: 例如:

// 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;

For classes and structs, if the type is unknown, then we need a forward declaration so that the compiler has some idea of what it is. 对于类和结构,如果类型未知,则需要一个前向声明,以便编译器对它的含义有所了解。 But we can combine it with the declaration, like so: 但是我们可以将其与声明结合起来,如下所示:

// In header file:
extern class AnimationController* g_pAnimationController;

Or written more verbosely: 或更详细地写:

// In header file:
class AnimationController;
extern AnimationController* g_pAnimationController;

Update for comment question: 更新评论问题:

#include <map>
#include <string>

// Declare AnimationCallback
extern std::map<std::string, AnimationCallback*> g_mCallbackMap;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM