Need help with C++ and classes
tfox at knology.net
Thu Nov 6 23:28:00 CST 2008
OK, I readily admit it, I am NOT a professional programmer. I have hacked
my way through assembly language, a little Basic, along with C and some C#.
Please excuse me if a specific term is not used 100% accurately.
But, I need some specific help with C++ and classes. The software that I am
writing is made up of several smaller functions (methods), each in a
separate class and CPP file. I want to share variables or their values
between these classes. I'm using QT on Linux. I have a main(), which
instantiates "class 1", which is the actual main function that sets up the
GUI and runs everything else. As necessary, it instantiates "class 2",
class 3", "class 4" as called for, each of these do specific tasks.
Since class 1 knows all about the subsequent classes (because it needed to
in order to instantiate them), it can gain access to all variables and
methods declared public in those other classes. My problem is that the
reverse is not true. Those other classes cannot gain access to the class 1
variables or methods, even though they are declared public.
I understand the preference for using setParam and getParam method in a
class for outside access to local variables. I can use get and set from
class 1 to access variables or methods in class 2, 3, 4 etc. But, I've
tried the exact same get/set code in class 1, and class 2, 3, 4, etc CANNOT
"see" those methods. I get compile or link errors.
I suspect that part of this is that class 1 is instantiated in main.cpp, and
there is no way to make that instance "public" to the rest of the classes.
I made a class1.h header file, which shows what a class 1 class should look
like, but cannot show where the specific instance of the class 1 of interest
is declared (inside main.cpp).
I want to do two things that I cannot right now:
1) Share a large array of data in memory (FFT result data of say 4096
floats) with multiple classes. Doing the set/get class thing seems like a
waste of time and processor cycles, especially if a copy is involved. This
array of FFT data points would be filled by one class (the data "getter"),
and made available to multiple other classes. These other classes will use
these FFT points to display the FFT data in various fashions (spectrum,
histogram/waterfall, 3d waterfall, etc) on the screen. I don't want one
huge file/class for all this, but want to break the work into several
smaller ones, one class per type of data display. BTW, the actual display
"windows" are created in the GUI in class 1. In the olden C days, I would
just use a global array, but that is frowned upon in C++.
2) have a single place to store & recall configuration settings. This
allows all the initialization, storage, and retreival of program-wide data
to be located within a single store & recall mechanism.
I'm stuck on this. Maybe I am overthinking it, or missing something basic.
I've thought about making class 1 just a very simple class, that calls a new
"main program" in class 1.5. That way, class 1.5's instance could be made
public in class 1 (if that makes any sense), while class 1 is the one
instantiated by main, and not generally available.
I'd be glad to get into more details on a one-to-one basis, not taking up
any more bandwidth here.
More information about the Tacos