Thread: Global Variables

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

    Global Variables

    I am working on a MDI application generated by the Appwizard in Visual C++6.0.

    The application has the usual document,view,dialog classes,etc. within it.

    I want to declare a global counter variable that will be incremented each time a new or saved document is opened. The aim here is to be able to check how many documents are currently open as I want to place a limit on that number.

    Does anyone have an idea on where "EXACTLY" I should declare and initialize my counter? I need a variable that can be accessed by my document class when a new document or saved document is opened in the application.

    Thanks for the help

    Tesita

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Well, first is first. A global variable is declared OUTSIDE of functions, hence a global scope. If you want to increment, then you just do that...increment.

    Look, global variables are time bombs waiting to go off. If you are only incrementing this variable in one function, you're better off setting it as a 'static' variable. That way it is safe and easy to debug on. But, if the variable is needed in multiple functions, go for it.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    24

    Global Variables

    Since I am working with Visual C++, I really wanted to know in which specific file I should declare the global variable. I want it to be incremented when a new or saved document is opened, which means being able to access it from OnNewDocument() and OnSavedDocument(). I have tried declaring it in the document object but this has not worked. I have also tried declaring it in the App object.

    Any ideas?

    Tesita

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Normally I declare the global in the source file that has WinMain, and then access it in the other sources using extern.

    You should be carefull with globals and only use them when there is no simple alternative and you are sure that it is safe. With a global, any function has access, so it gets to be slightly dangerous with complicated code. Have you thought of doing this with a static local varible?

    With games it is often prefrable to use globals for speed, but for a normal GUI you may want to try another method if possible. Surely there cant be many fuctions incrementing this count...or is there?

  5. #5
    Sayeh
    Guest
    In big projects, you might as well just created a file called 'globals.c' and the associated header called 'globals.h'.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I have a globals.h file. This is included ONLY in my MAIN.c file.

    In any other file I want to use the global you declare the variable as extern
    ie

    extern int iMDI_Page_Count;

    I make their names complicated and make sure I don't use any names similar to a global ever.
    Last edited by novacain; 12-04-2001 at 10:01 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM