Declaring Global Variables

This is a discussion on Declaring Global Variables within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm programming with Visual Studio Express Edition 2005 and my source code consists of many separate header files (.h) ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Declaring Global Variables

    Hi,

    I'm programming with Visual Studio Express Edition 2005 and my source code consists of many separate header files (.h) and .cpp that describe the various classes. I would like, then, to declare a global variable that would work on all of those classes and also on my main.
    Does anybody know how to do that?

    Thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    I suggest you avoid global variables and pass them through references between functions instead.
    But if you really must use a global variable, then define it is a .cpp file, then put an extern inside a header file and include that header where you want to use the global variable:

    Code:
    int a;
    extern int a;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Thanks, Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  2. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 09:53 PM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 03:23 PM
  4. Global variables, or...
    By Vber in forum C Programming
    Replies: 4
    Last Post: 01-03-2003, 02:49 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

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