By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

non-static global variables and the extern qualifier

P: n/a
A
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable

class Foo{
...
};
The scope of i is throughout the entire program since it is global and
non-static. Lets say i want to assign a new value to i in another file
called Bar.h, how is it done?

//Header File - Bar.h

i = 1 //like this? or must i use the extern qualifier here? confusion here
is that it i is global then we u need extern to make it external to another
file when it should be already available?

class Bar{
...
};
Regards,
A
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
A wrote:
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable

class Foo{
...
};
The scope of i is throughout the entire program since it is global and
non-static.
Right, but you defined i in a header, and if you #include that header in
more than one translation unit, you'll get a linker error for multiple
definitions. Rather define it in the .cpp file, and just declare it in
the header.
Lets say i want to assign a new value to i in another file
called Bar.h, how is it done?

//Header File - Bar.h

i = 1 //like this?
Well, not in the global scope, but within a function, yes.
or must i use the extern qualifier here?
No, but you must have declared it extern.
confusion
here is that it i is global then we u need extern to make it external
to another file when it should be already available?


You should do it something like this:

Foo.cpp:

int i = 0;
//...

Foo.h:

extern int i;

Bar.cpp:

#include "Foo.h"

void somefunc()
{
i = 1;
}

i is defined in Foo.cpp and is global, but other .cpp files like Bar.cpp
don't know anything about it. Just like you have to make a type or a
function from another tranlation unit known, you have to make a
variable known. The "extern int i" tells the compiler that there is a
global variable i of type int that defined elsewhere.

Jul 22 '05 #2

P: n/a
A

A wrote:
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable

class Foo{
...
};
The scope of i is throughout the entire program since it is global and
non-static.


Right, but you defined i in a header, and if you #include that header in
more than one translation unit, you'll get a linker error for multiple
definitions. Rather define it in the .cpp file, and just declare it in
the header.


Firstly, i agree with the error.

#include would obviously bring i into scope wherever you included it, and
thus defeats the purpose of the external qualifier? I gather from what Ive
read that by using the external qualifer you can open the scope to multiple
files - it does not make mention of also using #include.

I was thinking more on the lines:

Foo.h

int i = 0; // non-static global variable

class Foo{
...
};

Bar.h

extern int i; //refers to the i declared in Foo.h; no need to use #include

class Bar{
...
};
Regards,
A
Jul 22 '05 #3

P: n/a
A wrote:

A wrote:
> Hi,
>
> Consider this code:
>
> //Header File - Foo.h
>
> int i = 0; // non-static global variable
>
> class Foo{
> ...
> };
>
>
> The scope of i is throughout the entire program since it is global
> and non-static.
Right, but you defined i in a header, and if you #include that header
in more than one translation unit, you'll get a linker error for
multiple definitions. Rather define it in the .cpp file, and just
declare it in the header.


Firstly, i agree with the error.

#include would obviously bring i into scope wherever you included it,
and thus defeats the purpose of the external qualifier?


Why?
If you write:

int i = 0;

it lets the compiler know there is an int named i _and_ that it should
allocate space for that int.

extern int i;

also lets the compiler know there is an int named i, but it does _not_
allocate storage for it. So the extern just means "there is a variable,
but it alredy exists somewhere else". The linker will resolve it then.
I gather from what Ive read that by using the external qualifer you
can open the scope to multiple files - it does not make mention of
also using #include.
I was thinking more on the lines:

Foo.h

int i = 0; // non-static global variable

class Foo{
...
};


Well, this would only be ok as long as you #include "Foo.h" only _once_
in your whole program. But that would defeat the purpose of headers.
Bar.h

extern int i; //refers to the i declared in Foo.h; no need to use
#include
Right. But headers are just there to make things from one translation
unit known to another one. You put it all in a header so that you don't
need to repeat everything wherever it is needed. And just as you would
put a function declaration in the header and define it in the .cpp
file, you should only declare your global variables (and that's what
the extern actually means) in the header and put the definition in
the .cpp file.
class Bar{
...
};


Jul 22 '05 #4

P: n/a
On Wed, 26 Nov 2003 21:32:53 +1030, "A" <A@iprimus.com.au> wrote in
comp.lang.c++:
Hi,

Consider this code:

//Header File - Foo.h

int i = 0; // non-static global variable
Actually, i is defined without the static keyword, but it is indeed a
static object, meaning that is has static storage duration.
class Foo{
...
};
The scope of i is throughout the entire program since it is global and
No, you are confusing scope with linkage. The definition of i has
namespace scope in C++, and since it is at top level in the global
namespace, it is basically the same as what is called "file scope" in
C, even though that terminology is no longer used in C++.

The definition is in scope from the point where it appears in the
translation unit until the end of the translation unit, although it
may be hidden by another usage of the same name in an inner block.
non-static. Lets say i want to assign a new value to i in another file
called Bar.h, how is it done?
You can only assign a value to an object in code. You can only
initialize an object in its definition. So if i is defined in a
source file, you can't initialize it anywhere else, and you must
assign to it from inside a function, not at file level.
//Header File - Bar.h

i = 1 //like this? or must i use the extern qualifier here? confusion here
is that it i is global then we u need extern to make it external to another
file when it should be already available?

class Bar{
...
};
Regards,
A


You have some confusion over three separate but somewhat related
concepts, scope, linkage, and storage duration. Part of the confusion
stems from the fact that C++ inherited two different meanings of the
"static" keyword from C, not to mention adding another meaning of its
own.

Any object defined outside of a function has static storage duration,
which means it exists for the life of your program. Adding the
keyword "static" or the keyword "const" to such an object does not
change the storage duration, which is still static, and does not
change the scope, which is still to the end of the translation unit,
it merely changes the linkage from internal to external.

External linkage means that code in another translation unit may refer
to the object by name, provided that that code has a suitable external
declaration in its scope.

And the definition of translation unit is basically the source file
you pass to the compiler plus everything it includes.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #5

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote...
[...]
Any object defined outside of a function has static storage duration,
which means it exists for the life of your program. Adding the
keyword "static" or the keyword "const" to such an object does not
change the storage duration, which is still static, and does not
change the scope, which is still to the end of the translation unit,
it merely changes the linkage from internal to external.


Vice versa, actually. "static" changes linkage from external to internal.

Victor
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.