c++ global object

后端 未结 1 1641
抹茶落季
抹茶落季 2020-12-06 18:18

I wanna create a global object in cpp program, how do I do that? Is this right? in \"global_obj.h\"

#include \"class.h\"
Class obj;

in \"ma

相关标签:
1条回答
  • 2020-12-06 18:57

    We declare our globals as extern in a header file, in your case: global_obj.h, and the actual global variable in a source file: global_obj.cpp. In separate source files we #include "global_obj.h" to have access to them.

    It should look like this:

    global_obj.cpp

    Class obj;
    

    global_obj.h

    extern Class obj;
    

    main.cpp

    #include "global_obj.h"
    
    0 讨论(0)
提交回复
热议问题