CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2004
    Posts
    8

    Global Variable across multiple files?

    Guys,
    yet another question.

    How do I use global varibales across multiple files.

    and where exactly do I define the "extern int count;" ?

    lets suppose I have the following

    myclass.h

    myclass.cpp

    mymain.cpp -> this is where the main() program exists.

    basically I want my class to update a global variable everytime its called, and then I want to be able to read that variable from my main as well ?

    any suggestions? I m trying the "extern" but getting some LINK error saying I have unsolved external symbols.

    pls help
    Thanks

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Don't use a global variable for this. Add a static member to
    your class instead.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Jun 2002
    Posts
    137
    in your header declare, for example:
    extern int GLOBALINT;


    in your .cpp declare, for example:
    int GLOBALINT = 1;


    If you want to use this global variable in other classes and etc, you have to include the header file which containes: extern int GLOBALINT;

  4. #4
    Join Date
    Feb 2004
    Posts
    81
    Exactly. And the error 'unresolved external variable' that you were receiving was because you didn't define the variable anywhere. extern keyword doesn't define a variable i.e. doesn't allocate memory for it but gives compiler a hint that the variable is defined somewhere else and it's safe to use it down in the file.
    while(true)
    cout<<"C++ is divine\n";

    Feroz Zahid

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by souldog
    Don't use a global variable for this. Add a static member to
    your class instead.
    You're right, static class member should be used. Is it me, or has there been an influx of "how do you use global variables" messages in the various C++ forums lately?

    I'm asking this because usage of global variables should be discouraged. But unfortunately, it seems that others are reading very bad C++ books, reading C programming (not C++) books, or being taught by persons who really aren't good C++ programmers. I have a 100+ module library, and nowhere in there is a global variable that is shared between modules.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 2000
    Posts
    118
    Global variables are a lazy programmers shortcut... As long as you don't define vast amounts of global variables you should be ok; it's not like a globally declared integer is going to cause harm

  7. #7
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Mostly, agree with the words above.
    But I don't like that kind of "dogmas" ("don't ever use or smth")... for each rule there can be exceptions
    "UNIX is simple; it just takes a genius to understand its simplicity!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured