Hello! I just started using classes, so no wonder If do something wrong.
I made simple test that has MainClass and SubClass, SubClass being nested inside public section of MainClass.
Here is the code:
class MainClass {
public:
class SubClass {
public:
byte subVariable;
SubClass(byte subVariableIn) {
/*
Constructor for SubClass
*/
subVariable = subVariableIn;
}
}; //End of SubClass class
byte mainVariable; //should be inherited to SubClass as well, default value 5
MainClass(byte mainVariableIn) {
/*
Constructor for MainClass
*/
mainVariable = mainVariableIn;
}
};
MainClass mainInstance(3);
MainClass::SubClass subInstance(2);
MainClass::SubClass subInstance2(1);
void setup() {
Serial.begin(9600);
}
void loop() {
delay(1000);
Serial.println(mainInstance.mainVariable);
Serial.println(subInstance.subVariable);
Serial.println(subInstance2.mainVariable);
}
Error I'm given:
How come mainVariable is not accessible, since it should be inherited from MainClass?
C:\Users\Atte\AppData\Local\Temp\.arduinoIDE-unsaved20241131-28732-1l8i39u.5osr\sketch_dec31a\sketch_dec31a.ino: In function 'void loop()':
C:\Users\Atte\AppData\Local\Temp\.arduinoIDE-unsaved20241131-28732-1l8i39u.5osr\sketch_dec31a\sketch_dec31a.ino:37:31: error: 'class MainClass::SubClass' has no member named 'mainVariable'; did you mean 'subVariable'?
exit status 1
Compilation error: 'class MainClass::SubClass' has no member named 'mainVariable'; did you mean 'subVariable'?
your comment is not correct. The issue you're encountering is due to the fact that mainVariable is not inherited by the SubClass because it is a member of the outer MainClass rather than a member of SubClass itself. C++ does not automatically inherit non-static members of the outer class (like MainClass) into the nested class (SubClass).
your notion of subClass is possibly wrong. SubClass is not what is usually called a subClass of MainClass (it does not inherit from it), it's just a nested class with its own life (but exists only in the context of the MainClass)
did you mean something like (typed here, untested)
class MainClass {
public:
byte mainVariable; // Variable that will be inherited by SubClass
MainClass(byte mainVariableIn) : mainVariable(mainVariableIn) {
/*
Constructor for MainClass
*/
}
};
class SubClass : public MainClass { // SubClass inherits from MainClass
public:
byte subVariable; // Variable specific to SubClass
SubClass(byte subVariableIn, byte mainVariableIn)
: MainClass(mainVariableIn), subVariable(subVariableIn) {
/*
Constructor for SubClass
*/
}
};
MainClass mainInstance(3);
SubClass subInstance(2, 5); // Pass both mainVariable and subVariable values
Thanks! Sorry for late reply.
Only minor issue I find with this method, is that it requires re-typing every single of variables. And, it also creates additional copies of "MainClass", which in turn increases memory usage, which can be issue if using arrays.
In my current project, modbusRTU network I have one master device, which has RX/TX arrays to hold/transmit bytes, and there is no point in having multiple masters if only one is doing actual work.
I had in mind in using methods of this MainClass, that has all methods (namely function code queries), and different devices are separate subClasses.
I could use external arrays that are defined in main sketch, and use pointer with MainClass.
I have feeling I should create another thread for the project, since it might cause this topic to get messy. Just figured to share background of why this chaining of classes arose.
Is that a big issue ? and it’s only what is required for the constructors.
Well if you want inheritance then of course you have the whole hierarchy but I suppose that this is desired otherwise you wound not create a subclass in the first place.
Are you sure you understand what it means in terms of memory and what inheritance is for?
Yes probably another thread with clear explanation of the need and what you intend to do as OO model would seem appropriate. Study also the Stream class and all have a look at classes inheriting from Stream like HardwareSerial.