Global Variables?

This is a discussion on Global Variables? within the C++ Programming forums, part of the General Programming Boards category; I know a lot of people ay that global variables are bad practice, but it looks like I might need ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    Global Variables?

    I know a lot of people ay that global variables are bad practice, but it looks like I might need them. The problem is with my TTT Game. I have two functions that seem to be causing this...

    ShowBoard() and MakeMove().
    Because MakeMove() requires char board; I need to define it in the function. But ShowBoard (obviously) also requires char board; so I have to re-declare char board; in both. Which means that ShowBoard() keeps the old version, therefore the board is never really updated, so would I be right in saying that global variables would be a good idea in this situation? I Could post some code if you wish, but I don't think it's neccessary.

    Edit: How would I go about declaring a global variable?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Neither do I. By all means carry on using global variables. Chances are your current project is either small enough not to have to worry about it, or it'll become an unmanagable mess (in which case you won't have to post stating you've heard that global variables are bad practice).

    If you really want to program using "good practice" then you could look up pass by reference (or since we're in a C++ forum, OOP).

    >Edit: How would I go about declaring a global variable?

    Declare it outside any function.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    99

    detail?

    How exactly would I do that? Could you show me some code?

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    More specifically you could create a class called TicTacToe ( for example) and define char board within that class along with ShowBoard() and MakeMove(). That way they both would have access to char board. However to keep with "good practice" you should create daughter classes, one for one player and the other for the computer with more specific member declarations. This would probably be the more structured way of creating Tic Tac Toe in C++.

    Edit:

    class CTicTacToe //create the class
    {
    protected: //allows derived classes to access

    char Board[3][3];

    void Showboard(void);
    void MakeMove();
    };

    void CTicTacToe::Showboard(void)
    { //whatever you want
    }
    void CTicTacToe::MakeMove()
    {//...your code here
    }
    int main()
    {
    CTicTacToe ttt; //create an instance
    ttt.Showboard(); //call the function
    return 0;
    }

    just a quick example but I hope it helps.Oh yeah the daughter classes. Same way, well almost that you created the TicTacToe class:

    class CPlayer : CticTacToe // Now Cplayer can access any
    // members of CTicTacToe
    {
    //whatever you want
    };
    Last edited by Traveller; 05-03-2002 at 07:08 PM.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Thanks Heaps, I was Just thinking about classes, and making my functions a member of it. I'll start on that right away, thx again!

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    Ok, For now I've decided not to use functions, although my codemay get messy, I'll always be able to seperate it into functions, at the moment, without the functions it's working great.

  7. #7
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Global variables are frowned upon in C++ because it's not good Object Oriented Programming practice, like Sorenson mentioned.
    Most of the time there are ways around it, but in game programming I think you see quite a bit more use of them. Using classes is a good start. If you take a look at most of the DirectX samples you'll see that the main objects of the programs are declared global. This relieves a great deal of headache and is typically OK due to the nature of the application.
    Game programmers, in general, tend to push the limits of good program practice in order to get the job done.
    Try and avoid them if you can (by using other methods of data passing) but it's OK if you end up using a few to make life easier.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 10:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 02: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, 05:37 PM

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