c++: external variables

Discussion in 'C++' started by Newsnet Customer, Aug 22, 2003.

  1. 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
     
    Newsnet Customer, Aug 22, 2003
    #1
    1. Advertising

  2. Re: external variables

    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
     
    Buster Copley, Aug 22, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Deb M.

    Post Variables to External Script

    Deb M., Jul 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    919
    David Waz...
    Jul 11, 2003
  2. Adam Bozanich

    external variables

    Adam Bozanich, Apr 20, 2004, in forum: C++
    Replies:
    6
    Views:
    11,530
    Buster
    Apr 21, 2004
  3. Scott Allen
    Replies:
    8
    Views:
    10,957
    Scott Allen
    May 2, 2004
  4. Replies:
    9
    Views:
    973
  5. Mellow Crow
    Replies:
    6
    Views:
    421
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page