Declaring Global Variable Inside a Function

Nov 19, 2008 at 9:08pm
How do you declare a global variable inside a function.

The reason for being able to do this is so that I can use a function to initialize a bunch of things (keeping the main.cpp file clean and small).


Functions.hpp
1
2
3
4
5
6
7
8
9
10
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void GameInit()
{
    //Initialize "instances list"
    std::vector <GameInstance> objDB;
}

#endif FUNCTIONS_H 


Main.cpp
1
2
3
4
5
6
7
8
9
10
#include "Functions.hpp"

int main()
{
    GameInit();

    ObjectBall myObj;

    objDB.push_back( myObj );
}


ObjectBall would be derived from GameInstance.
Nov 19, 2008 at 9:11pm
You cant. The whole point of a global variable is that it is valid in the whole code, and doesnt belong to a function. If you declare a variable inside a function, it is always a local variable inside that function (I dont know anyting, so i could be wrong, but i dont think so)
Nov 19, 2008 at 9:24pm
So would classes be the only viable solution?

1
2
3
4
5
6
7
8
9
10
class GameInit()
{
    public:
        std::vector <GameInstance> instances;

        void add( GameInstance &g )
        {
            instances.push_back( g );
        }
};


1
2
3
4
5
6
int main()
{
    GameInit game;
    ObjectBall myObj;
    game.add( myObj );
}
Nov 19, 2008 at 9:32pm
I dont completly understand what you're trying to do. If you want a global variable, simply declare it outside a function (normally after the include's and "using namespace std;" statements), and you can use it in every single function.
If you want to avoid using global variables, you could use pointers
Last edited on Nov 19, 2008 at 10:17pm
Nov 19, 2008 at 10:05pm
I think he is worried about the common anti-global-variable sentiment.

There is nothing wrong with using a global variable --if your variable needs global scope.

Silly things like "never use globals" and the like are nonsense. The correct way to think about it is to place things in the appropriate scope.

No one complains about functions like printf() or classes (ie "variables") like cout being placed in a global scope. Why? Because that is where they are useful.
(Ok, ok, some people complain about it, but most of them know enough about it that it is almost always reduced to academic banter.)


In short: for things that need to have global scope, use global variables/functions/etc. That is, if you need to access a value in a global context, that value should have global scope.

For things that don't need global scope, use namespaces/local blocks/etc.


Program options are an excellent choice for making a global object. By definition, the options affect the operation of the entire program --and hence, global access is required.

Purists who think you should pass such things around as routine arguments are the same people who like playing with monads and who think that Schrödinger's cat is doing exactly what they think he is, a priori. In real business situations, such thinking is a money pit: it wastes time and complicates software design --it is a disservice to your employer/teammates/whatever.

Use globals when you have to. (But no more.)


Hope this helps.
Nov 20, 2008 at 12:48am
I think if your looking at global variables you should consider an alternative design solution. Build a singleton class called "Configuration" etc and store them in that. That way you can validate the usage and assignment of these variables etc. Functionality not offered with global vars.
Topic archived. No new replies allowed.