Whats the best way to control a global class?

This is a discussion on Whats the best way to control a global class? within the C++ Programming forums, part of the General Programming Boards category; I'm developing software that computes many different types of fractal equations and draws them to the screen using OpenGL. Different ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    Whats the best way to control a global class?

    I'm developing software that computes many different types of fractal equations and draws them to the screen using OpenGL. Different types of equations such as Complex Fractals, Cellular Automata, Iterated Function Systems, number theory, etc... and thus I have a .h and .cpp for each type of equation that I plan on implementing. Within each header is a class defining the branch of equations, this is a very simple example:

    IFS.h
    Code:
    #ifndef _IFS_
    #define _IFS_
    
    class IFS
    {
    public:
         IFS(){
              Iterations = 50000;};
         ~IFS(){};
    
         int Iterations;
    
         void SierpinskiTriangleGenerate();
         void InvertedTriangleTreeGenerate();
         void PentagonGenerate();
         void Fern)();
    };
    
    #endif
    then in IFS.cpp the functions for computing the equations would follow of course. Simple, yes quite. But there is a confusion penumbra that I've meet and this is why:

    I create a version of this class in main.cpp, something like this:

    main.cpp
    Code:
    #include <blah blah blah.h>
    #include "IFS.h"
    
    bool main()
    {
         IFS *IFSClass = new IFS();
         // Declare all other windows and classes...
    }
    Now, this class is accessable to main() function and main() function only... I have the GUI that needs to malliate IFSClass, I have threads that want to perverse it and other equations that use it for benchmarking etc... So the problem lies in, how do I make this globally accessable?

    Now I know that I can use extern to make this specific class nice and usable for all other code, but it's not just this class that I need to make global. Heres the heart of my problem:

    When I have a function that I pass many arguments to to initialize something, like a window:
    Code:
    Window *mainWin = new Window("Main Window", (parent data, blah blah blah));
    I also need this class to be global to all other classes, to access the pointer to the window so the OpenGL functions can malliate it etc... But if I try to extern this before the start of main() all the arguments that are pointers to all different types of classes and structures that are created during the initialization of main() get FUBAR'd because they obviously havn't been created yet.

    Now I may be making this so extremely complicated that I need to rework how I'm flowing this code entirely but this is EXACTLY what i'm trying to learn how to do. I'd like a consice review of how either articulant or slobbish my methods of global class declerations are and what better methods are and what I should learn to become a better programmer

    I give this pseudo-flowchart to explain my theology of global mass-read classes and how I initialize them:

    1. Create within the main() function a version of the class that I wish to make global
    Code:
    Class *NewClass = new Class(*args);
    2. Then because I have so many global classes I create a single global class to take care of them all and pass my pointers to that declared class:

    Pointers.h
    Code:
    class IFS;
    class Complex;
    class WindowScreen;
    
    class Pointers
    {
    public:
         IFS *IFSLink;
         Complex *ComplexLink;
         WindowScreen *WindowScreenLink;
    };
    3. Then in main() I initialize my main functions and pass the pointer to the Pointers class containing all global class pointers:
    Code:
    #include "Pointers.h"
    
    bool main()
    {
         Pointers *pointers = new Pointers();
         pointers->IFSLink = new IFS();
         pointers->ComplexLink = new Complex();
    
         Window *NewWindow = Window(blah blah blah, *pointers);
    }
    4. After this is all said and done I can grab my IFS and Complex global variables from my new window class through the pointers argument, which points to another class containing global class pointers.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Once I needed to do a similar thing and that is they solution I used.
    edit: If you have a lot of pointers and don't want the added lines of code in main, you can always make a constructor

    Code:
     Window *NewWindow = Window(blah blah blah, new Pointers());
    
    class Pointers
    {
    ...
      Pointers()
      {
        pointers->IFSLink = new IFS();
        pointers->ComplexLink = new Complex();
      }
    };
    Last edited by C_ntua; 11-12-2009 at 01:08 AM.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    thanks c_ntua for the reply, but is this really the best way of doing things? I mean, is the mass pointers class really the thing to be doing? Is it clean cpp wise I mean

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    I don't like the notion of a global collection of objects at all.

    I remember working on a smallish project in which a team leader created a global object named (unimaginatively) Globals, which included pointers to about 500 other objects of about 40 distinct class types. That program became a maintenance nightmare for a couple of reasons.

    Firstly, because everything (he insisted) was needed was in "Globals", anything he needed to share was placed in it. It therefore became quite challenging to understand what several class functions did when they worked by manipulating several global objects. Eventually, it reached a point where a small change on one function could cause other, seemingly unrelated, functions to break because of a chain of interactions with other functions that accessed some common global objects.

    Second, even though this was a small project, the use of "globals" meant there was one header file that included a considerable number of others. That globals header was touched almost routinely when new objects were added to the "Globals" pool, and was also #include'd by most source files. This meant that even small changes of functionality could trigger very large rebuild times - it is a pretty mean feat to have a small project of about 200K lines requiring most of a working day to compile.

    In practice, I suggest minimising use of global objects (ideally, eliminate their use completely). Better to do a bit more work to ensure that all functions receive information they need as arguments (extra pointer or reference arguments). That makes the functions more controllable, and also more flexible in terms of being able to act on any object of a given type. If it becomes necessary to pass several objects as an argument, it is not difficult to create a class/struct container to manage the interactions.

    The trade-off is a bit more work to design functions that are self-contained, but less risk of having code break because of interactions that aren't obvious.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Change a control class during runtime
    By Joelito in forum Windows Programming
    Replies: 3
    Last Post: 01-12-2006, 01:13 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM

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