Regarding global variable.

This is a discussion on Regarding global variable. within the C++ Programming forums, part of the General Programming Boards category; Hello. I have declared a boolean global variable within a header file. Now, I want to change value of global ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Regarding global variable.

    Hello.
    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.
    Regards,
    Sunny.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21