Thread: Regarding global variable.

  1. #1
    Registered User
    Join Date
    Sep 2007

    Regarding global variable.

    I have declared a boolean global variable within a header file.
    Now, I want to change value of global variable within a function in another file such that it is globally changed.
    Thanks in advance for your answers.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    If you want to go that route, you really should declare the variable as being extern in the header file, and redeclare it (without the extern) in the appropriate .cpp file that implements that header. After you do that, you will be able to alter the variable elsewhere if you include that header file.

    A more appropriate way of handling this would be to disallow other parts of your program from seeing the boolean variable. What they should be able to see are two functions, one that sets the variable and one that gets the value of the variable. If you need to add debugging information later on, you can control how other parts of your program interface with the variable at those two places instead of tracking all throughout your program for such changes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 03:25 AM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. global variable turns zero
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 12:52 PM
  4. Global variable???
    By loopy in forum C Programming
    Replies: 13
    Last Post: 06-09-2002, 03:08 PM