Thread: Variables in a header file

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    162

    Question Variables in a header file

    When should I use static and when should I use extern? And can I not just declare them like normal? Let's say I have two .cpp files in a project I want to access the same bool variable from a header file. How do I declare the bool variable? Thanks for your help!

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You either declare the variable as extern (defined elsewhere), or const in header files. Neither are what you want. Header files shouldn't be used for what you're trying to do.

    Put the variable in one of the cpp files with external linkage.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    First, if you declare a variable in a header file, it will be instansiated in every file that includes it. IE if included in two files you will get two instancies of it.

    A static variable has local scope but is maintained in the global area of the stack. If declaring a global value static, its scope will be limited to the methods defined in the same file.

    An extern variable is a definition of a global variable that is allocated somewhere else.

    Do this:
    - in the header file declare the variable with the extern modifier
    - declare the variable globally in ONE of the source files

    Then the methods in the files that include the header file will have access to the variable.



    Here is an example of using static:

    int setVariable(int x)
    {
    static int valueToKeep= NULL;

    if(x != NULL) valueToKeep = x;
    return valueToKeep;
    }

    int getVariable()
    {
    return setVariable(NULL);
    }

    In the above example, an integer value is available to all processes within an application without allowing any direct acces to it and it is not declared globally. (I just do not like global variables)

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    You use extern to DECLARE a variable.It tells the compiler to look for the DEFINITON of the variable in one of the included header files. No memory is allocated when you declare it using extern.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    look for the DEFINITON of the variable in one of the included header files
    Actually the linker looks for the definition in an object file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM