Thread: Avoiding Global Variables! How?!

  1. #1
    Unregistered
    Guest

    Avoiding Global Variables! How?!

    If I declare an instance of a class in say, submain.cpp and then try to refrence to that class in main.cpp, it gives me an error.

    Well how the hell am I supposed to do anything, if I can't even use one class in another file?

    For example:
    submain.cpp
    class Blah
    {
    void DoStuff();
    };

    ...

    Blah blahinstance

    And in main.cpp

    blahinstance.DoStuff;

    But it doesn't work! How can I use a class in another file, in another function without making it a global variable? Someone please help!

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    #include "submain.cpp"
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    extern

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    classes should be declared in an h file...
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  6. #6
    Unregistered
    Guest
    You guys are missing the point..

    I've declared my class, defined its functions, etc. in another file.

    Now in submain.cpp, I go:
    monster Ogre1;

    Lets say, for argument sakes. Now lets say, I have...

    death.cpp And I want my Ogre1 to DIE in death.cpp, I can't just go:

    Ogre1.death

    As my class function denotes, because its trying to use an instance of a class that was defined in a different function. So how can I use the instance of the class from the submain.cpp function, without making Ogre1 a global class?

  7. #7
    Unregistered
    Guest
    Please help, I don't understand what I'm doing with it. I don't see how OOP is useful, if you have to declare everything as a global variable.

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    " I don't see how OOP is useful, if you have to declare everything as a global variable."

    May wanna try private and protected ...

  9. #9
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Here is how it should be:

    In blah.h
    Code:
    class Blah 
    { 
       public:
          void DoStuff(); 
    };
    In blah.c
    Code:
    Blah::DoStuff()
    {
       cout << "Blah" << endl;
    }
    In main.c
    Code:
    #include "blah.h"
    
    int main()
    {   
       Blah BlahInstance;
    
       BlahInstance.DoStuff();
    
       return 0;
    }
    You then have to compile blah.c and main.c together. It depends on which compiler you use to know how to do this.

    Does that clear it up? :)

  10. #10
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    tried using extern?

    extern int myVar;

  11. #11
    Unregistered
    Guest
    Yes, BUT.. I have an instance of the class defined in another file.. and then in one file, it can't use that instance.

    To use your example,

    If I moved "Blah blahinstance;" to submain() in submain.cpp, now main() in main.cpp can't do blahinstance.DoStuff() because its not declared as a global variable, only submain() has access to it.

    So how could I make like a pointer to the instance or something in main() so that main() can use the exact same instance as submain()?

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You are declaring it in another function and therefore, it goes out of scope by the time it returns from that function. Try making it static and see if that works.

  13. #13
    Unregistered
    Guest
    Want to lend a little code to this "static" decleration? I can't figure it out.

  14. #14
    Unregistered
    Guest
    Pretty please? I'm so bloody lost.

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    To really make use without global variables, you need a place to keep all your important pointers. For example:

    If you create a game, the game should be runned in the "CGame class", and the CGame class holds the pointers to all your little monsters and maps and everything. The CGame is instanciated from main.

    Code:
    class CGame
    {
        void create()
        {
             // create ogres and put them in the list here.
        }
    
        void run()
        {
            m_bRunning = true;
            while(m_bRunning){
                 //update all your oggres here.
                 for each ogre in m_OgreList{ Orge->Update() }
                 KillOgre(GetNextOgreFromList);
                 if(IsAllOgresDead()) m_bRunning = false;
            }
        }
    
         void KillOgre(COgre *pOgre)
         {
              pOgre->DieBastard();
    //          remove pOgre From list
         }
    
         LinkedListWithCOgres m_OgreList;
         bool m_bRunning
    };
    
    void Main()
    {
        CGame game;
        game.create();
        game.run();
    }
    I've keept this very very simple, and I hope you get the Idea how to use OOP to get rid of the globals and make it all nice and confy. Now, I don't say "put every pointer in CGame", because that is wrong also. You should rather use the model in KillOgre, that you pass a pointer to the function, telling it what ogre we are talking about. And the Ogre should hold its pointers to its "What-ever-stuff-the-ogre-is-using".

    Another way of doing this kind of "get around globals" is to use something called singleton. But that is only usable for say a Game Engine, where you know that you'll only be using one, and more should never be created. Singletons can only have one instance at a time. This is usefull since you can get this one-and-only instance by just creating an ordinare pointer to the class. I won't explain singletons more here though, think you should manage the OOP part first!

    Well I've tried to keep it simple, and this is only the basics. Probably broke a few "rules of OOP" to... Love to do that...

    Where you go from this simple OOP design is up to you. I would go for polymorphism and use of interfaces, templates and more.

    for example this is how the core of my engine would look (the update/moveframe part):

    Code:
    void CEngine::Update()
    {
        // pseudocode for iterating trough a linked list
        for each pGameObject in GameObjectList{
             pGameObject->Update();
        }
    }
    
    
    ----
    meanwile, faraway in the CGameObject class
    
    class CGameObject
    {
        virtual void Update() = 0;
    };
    What happened there? isn't that the same code as before? No, looks the same but is not. The game object is the base interface for all object I have in the game world (player, map, sword, invetory, chatwindow ...) All of these object inherit the GameObject class and implements the Update function. for example the player needs to update his position, the chat window need to update its text buffers. This way, i have only a single object to handle in the core of the engine, yet, i can update any object in the game from there...

    Well I won't digg deeper from here. This topic is huuuge and I've already confused you enough I think... Since you are still working with simple classes. Ask me if you got problems.

    Happy coding!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 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