c++: external variables

N

Newsnet Customer

Hi,

Is an external variable a variable that is defined outside of a method? or a
variable with the extern modifier prefixed to it? and what's the difference
with external and global variables?


cheers
-


---
Replace i with a if you want to reply by e-mail

Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
 
B

Buster Copley

Attila said:
Newsnet said:
Hi,

Is an external variable a variable that is defined outside of a
method? or a variable with the extern modifier prefixed to it? and
what's the difference with external and global variables?

[SNIP]

The traditional definition of an external variable is a global variable,
defined in another source (implementation) file - brought into view by using
an extern declaration.

I'm not sure. How about this: 'global' is a namespace. A variable
defined at namespace scope is available from any translation unit,
provided a declaration has been seen, and can therefore be loosely
described as a 'global variable' (unless the namespace is unnamed).
'external' is a storage class which describes any declaration at
namespace scope which is not also a definition. External declarations
are often necessary for 'global variables' because C++ has a
one-definition rule.
BTW, one really stupid question to anyone who has ever done something like
this. How do you declare an extern variable, which is in a namespace? I
never needed it yet, but this topic made me wonder. Is it this:

namespace Whatever {
extern int i;
}

?

Yes. You can put that in a header. Then 'int Whatever::i;' or 'namespace
Whatever { int i; }' in one translation unit (with or without an
initializer).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top