Where does one declare objects and methods in basic GUI programming?

123 views Asked by At

I'm coming from a small background in C++ command-line application and have recently started to study GUI programming via the JUCE framework. I've successfully built a few GUI apps, some being ports of older command-line app code, but I wanted to verify if I was going about it correctly not. In command line applications, we do a lot of declaring of variables in the main function and obviously, locally in some methods, but what is the equivalent in a GUI app? For instance, if I'm working with a single window GUI app, with just one GUI class and I'm wanting to declare some parameters that are access by all parts of that GUI, do I declare them somewhere in the main, or in the private section of the class specification? I've been declaring all my variables in the private section of the GUI class, same as I would with a normal class, but I wasn't sure if this was correct or not.

What about if I wanted to share a variable between 2 GUI classes? At what point / scope do I declare that so that both have visibility of those parameters?

This is my first attempt at a GUI app, so I accept that I probably made a TON of mistakes, (its not finished in functionality yet):

https://github.com/JosephTLyons/The-Lyons-Den-Encryption

1

There are 1 answers

2
MSalters On BEST ANSWER

Actually, we don't usually declare that many variables in main regardless of the type of application.

Those "parameters accessible by the whole application" can be members of a singleton class, for instance.

In general, you look at the reason why two classes need to share a variable. Answering the question how do two classes cooperate is often easier when you know why they cooperate. E.g. if class B is a helper class for A, every B instance exists solely to help a single A, and B's lifetime is therefore solely determined by its A object, then A should just own B, and can access all of B's member variables (via B's member functions, of course, not directly. There's a good reason why C++ classes default to private:)