Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring global variables in Visual C++ applications

Status
Not open for further replies.

kathas

Programmer
Nov 28, 2006
6
DE
Hi all,

The problem i have is in declaring variables the values of which i will be able to access throughout my whole application (Global). No matter the class of which the function derives.

I tried doing it with namespace but the namespace can only be accessed by the members of the class under which it has been declared.

All help is welcome thanx a lot,

Kathas
 
Declare & define the variable in one file, then declare the global variable using the extern keyword in other places where it will be used; or even better in a header file that's included in the other files.
 
Similar to cpjust. I normally do it like this. Some people like others hate it. The big advantage is that all the globals only live in the header file and nowhere else. You only need to modify one file to add it in.

Source file
Code:
#define global_cpp
#include "global.h"

Header file
Code:
#ifndef global_h
#define global_h

#ifdef global_cpp
#define EXTERN
#define DATA(x) = x
#else
#define EXTERN extern
#define DATA(x)
#endif

// Ordinary globals
EXTERN int anInteger;

// Preset values
EXTERN const int constance DATA(200);

#undef EXTERN
#undef DATA
#endif
 
Thanks a lot guys...

Already done it without globals...

If i told you the case you'd propably tell me not to do it with globals too.

Thanx for answering though...

Kathas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top