c++: external variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Newsnet Customer

    c++: external variables

    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


  • Buster Copley

    #2
    Re: external variables

    Attila Feher wrote:[color=blue]
    > Newsnet Customer wrote:
    >[color=green]
    >>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?[/color]
    >
    > [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.[/color]

    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.
    [color=blue]
    > 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;
    > }
    >
    > ?[/color]

    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).
    [color=blue]
    > --
    > Attila aka WW[/color]

    Comment

    Working...