Sharing data between 2 files

Hello all, I'd like some design advice. The problem is I have a function which sets some variables and I want to access the variables, but myfunc() is nested too deeply for me to pass a data structure all the way down and to return all the way up. The functions reside in different files. I'm not allowed to use extern structs (i.e. a global variable).

The advice I was give was to use a class and instantiate it in myfunc(). My solutions are:

- using the singleton class method
- static variables and function residing in the class (but I'm suspicious of this way. seems like it's just class variables masquerading as global variables.

Any suggestions would be appreciated.
Could you give more insight about the functions that need to share data? This sounds like a design flaw and not like a problem that needs solving.
My takeaway from this is that maybe you need structure x that's defined in file a to be available to function y in file b?

Seems overly complex. If that's what you're looking for, you can just include the header file where you defined the structure in the .cpp file where you defined the function, and create an instance of the structure inside of the function y.

I am, of course, assuming that you do actually define structures in your header files, and not in the source files. Defining a struct in a source file would not make sense to me, given that a structure is a custom data type, and therefore not much different than an int or char in most aspects.

If, however, you're trying to pass an instance of a struct containing specific data to a different function, then, as long as the header file that's got the definition of your struct (again, see the assumption above) is included in the source file with the function, you can pass a struct between functions exactly the same way you can pass an int or char between functions (again, structs are just custom data types).

It also occurs to me that your function y that's supposed to be receiving the variables is called by a different function than where the variables are normally accessible. If that's the case, you can simply create another function whose only responsibility is to actually obtain the data information and pass it to a third-party function, as it were. Then you can just make the function call to y, and pass the data retrieval function to the function y as a parameter.

If any of this is applicable, I hope it can solve the issue you're having.
Topic archived. No new replies allowed.