Monday, February 25, 2008

Accesing a veriable defined in another file in C++

This might be a very simple thing to most of the programmers. But I thought of writing this in my blog as I spent half a day finding a way to access a variable in .cpp from another .cpp(without importing the .h or defining as public inside a class.)

suppose the variable we want to access is defined as follows in the original file,

bool flag;

if you want to access this from another .cpp simply you have to do is define it with extern key word in that file,

i.e extern bool flag;

both declarations refer to the same variable "flag".


Janaka Priyadarshana said...
This comment has been removed by the author.
Janaka Priyadarshana said...

I think you have waste a half of day for nothing. ;-)