Turning a varible into a global.

This is a discussion on Turning a varible into a global. within the C++ Programming forums, part of the General Programming Boards category; Code: void make(hwnd HWND) { int nVarry = 90; } void read(hwnd HWND) { if(nVarry == 90) Beep(2000,100); } But, ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Turning a varible into a global.

    Code:
    void make(hwnd HWND) 
    {
    int nVarry = 90;
    }
     
    void read(hwnd HWND)
    {
    if(nVarry == 90)
    Beep(2000,100);
    }
    But, sense nVarry was declared inside of a function, a different function cannot read nVarry. So my question is, how can I make a varible (in this case 'nVarry') become a global varible so that it can be read through out the whole app, and not just inside of the brackets that it was created in?

    Thanks in advance, August.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, there are usually better methods than creating global variables, but to create one, you simply need to declare it outside the scope of any functions/classes. So, at the top of your file (generally below header includes -- stylistic issue), just declare it: 'int nVarry;'.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You don't wan tto make it global, why don't yo upass nVarry to each of the functions? Or make those functions members of a class and nVarry a member of it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  3. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  4. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  5. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 08:51 PM

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