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 “main.cpp”
extern Class obj;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
We declare our globals as
externin 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
global_obj.h
main.cpp