How to declare global variable outside a class?
How to declare global variable outside a class?
Create a header with global definitions.
Example: my_globals.h
Qt Code:
#ifndef MYGLOBALS #define MYGLOBALS int variable1; #endifTo copy to clipboard, switch view to plain text mode
Include the header everywhere you want.
Use extern keyword to avoid allocating some memory for yours global variables every time you include your header globalvar.h in multiple files. With this keyword you only allocate them once.
Qt Code:
// globalvar.h #ifndef MYGLOBALS #define MYGLOBALS extern int variable1; extern int variable2; #endif ... // globalvar.cpp implementation int variable1= 0; int variable = 0;To copy to clipboard, switch view to plain text mode
Last edited by toutarrive; 26th August 2010 at 09:33.
Bookmarks