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

c++: external variables

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Attila Feher wrote:
Newsnet Customer wrote:
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).
--
Attila aka WW


Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.