I need global access to some variables, how ?

I have the main classA, it creates instances of class B, this in turn creates instances of class C,
Imagine my class C ask for a value that I have to pass up to 'grandfather' classA.
How can I do this ?
Thanks
Lots of ways. Here's one.

You could give every instance of B knowledge of (e.g. a pointer) to the A that made it, and every C a pointer to the B that made it, and the classes could thus pass data back and forth to each other.
Last edited on
But, there is no way to write something like MY_GLOBAL VARIABLE inside class A, true ...?
False. You can create a static variable inside a class. Only one copy of that variable exists for all instances of that class.
So if I want to have a global variable for the whole project I have to pass it to the clasess I want to have access to this global isn't it ?
So at every constructor I'd have to pass every 'global variable', in example,
my_instance_of_classn = new MyclassN (..., globalvar1, globalvar2, etc )
Or maybe can be better to have an struct for my global structure , isn`t it ?
Thanks
Another option is to create a "singleton" object. That is, throughout your whole program, there is only one such object instantiated. Any section of code that tries to create one actually gets the same one that everyone else is using.
Topic archived. No new replies allowed.