Hello, tell me how to properly store a lot of device settings. There is a program for the device, where there are many-many channels (1,2,3, ... up to 20), well, for each channel there are different parameters (signal type, upper limit, lower limit, upper limit of measurements, lower limit of measurements and etc ....). Accordingly, each parameter for each channel should be constant, I created a class:

Channel1.h :

class Channel1Options { public: QString GetUnitsName(); int GetSignalType1(); int GetSignalType(); int GetLowerLimit(); int GetHigherLimit(); int GetLowerMeasureLimit(); int GetHigherMeasureLimit(); void SetSignalType(int newsignaltype); void SetLowerLimit(int newsignaltype); void SetHigherLimit(int newhigherlimit); void SetLowerMeasureLimit(int newlowermeaslimit); void SetHigherMeasureLimit(int newhighermeaslimit); void SetUnitsName(QString newunit); // приватные переменные настроек канала 1 private: static int signaltype; static int lowerlimit; static int higherlimit; static int lowermeasurelimit; static int highermeasurelimit; static int measureperiodsecond; static QString unitsname; }; 

Channel1.cpp :

 void Channel1Options::SetUnitsName(QString newunitname) { unitsname = newunitname; } QString Channel1Options::GetUnitsName() { return unitsname; } 

Next, for the second channel, I make a class that inherits the class for the first channel:
Channel1.h :

 class Channel2Options : public Channel1Options { // переменные настроек канала 2 private: static int signaltype; static int lowerlimit; static int higherlimit; static int lowermeasurelimit; static int highermeasurelimit; static int measureperiodsecond; static QString unitsname; }; 

I want to create objects for the class of the first and second channels, we can work with their parameters, respectively

main.cpp :

  Channel1Options a; Channel2Options b; a.SetUnitsName("Volts"); b.SetUnitsName("Ampers"); qDebug() << a.GetUnitsName(); qDebug() << b.GetUnitsName(); 

In the desired embodiment, the output should be:

 "Ampers" "Volts" 

But in fact:

 "Volts" "Volts" 
  • No And you do not need so many classes! Use the same class and regular fields. The immutability of the field does not mean that it should be static. - Pavel Mayorov
  • thanks, but ideally I want to do something like this: channel1.setunits ("Ampers"); channel2.setunits ("Volts"); - Andrey Shmelev
  • So why do you need two classes and static fields? - Pavel Mayorov
  • I do not know, I am looking for the correct way to store a set of parameters for 20 channels. Well, I want to do the work with their reading and writing visual - Andrey Shmelev
  • 2
    So why do you need two classes and static fields? - Pavel Mayorov

1 answer 1

The first point is that you do not need to inherit one channel from another, this does not correspond to reality. The second channel is not the first. If you really want to inherit - make a common base class (perhaps abstract).

Point two. Your [S/G]etUnitsName works with a static variable in the first class and they don’t know anything about the second variable, so everything works correctly. Inherit - inherit everything :)

But in general - I see no reason to do everything the way you do - through static fields. What for? A single channel is very necessary - make a singleton, but in fact, global variables, why produce? ...

  • I want to use one common base class - Andrey Shmelev
  • Then it is not Channel1 . It should contain only functionality common to all channels. And yet - the parameters of the channels are the same? Or completely different? Maybe one channel class and a collection of 20 elements make sense? How different is their functionality? And once again - channels 1, 2, 3 ... - are channels , but channel 2 is not channel 1 ... - Harry
  • As long as the channels have the same functionality, the parameters are the same (others will be in the future). - Andrey Shmelev
  • Then it should be one class, and your device - contain 20 copies of this class. - Harry
  • It is clear, I will try. Just scared. That the copy somewhere was not lost, and the data in it did not disappear. Certainly I will write down all parameters in a file. - Andrey Shmelev