Hello,
I'm facing an issue that i cannot solve.
I have a base class to drive some sensors and i derivate this class for specific sensors operations.
Consider the following simplified code :
/*sensorBase.h*/
class sensorBase
{
protected:
void begin(int);
void myOtherMethod(char);
}
//end
/*sensorBase.cpp*/
sensorBase::begin(int i)
{
Serial.println("i run in base class");
}
/* sensor2.h */
#include "sensorBase.h"
class sensor2 : public
{
protected:
void begin(int);
void myOtherMethod(char, int);
}
//end
/*sensor.cpp*/
#include "sensor2.h"
sensor2::begin(int i)
{
Serial.println("i run in derivated class");
}
sensor2::myOtherMethod(char c, int i)
{
serial.println("do more stuff than in base class");
}
//end
/*project.ino*/
#include "sensor2.h"
sensor2 s;
loop()
{
s.begin(1);
}
//end
Serial monitor says "i run in base class"
So my derivated class is never called.
I tryed to use virtual/override options, but i think i misuse them and it does not work too.
Can someone provide some help, direction, documentation to read or clarify this ?
#include "sensorBase.h"
class sensor2 {
public:
void begin(int) {
Serial.println("i run in derivated class");
};
protected:
void myOtherMethod(char, int) {
Serial.println("do more stuff than in base class");
};
};
sensorBase.h
class sensorBase {
public:
virtual void begin(int) {
Serial.println("i run in base class");
};
protected:
virtual void myOtherMethod(char);
};
Output
Monitor port settings:
baudrate=115200
Connected to /dev/ttyUSB0! Press CTRL-C to exit.
i run in base class
i run in derivated class
You don't define ANY derived class... Not at all sure what you expect to happen, but you clearly do not understand how to define a derived class, and it's not clear to me what you are trying to accomplish, much less what code you used to try.
Hello guys,
thank you so much for you remarks and advices.
I changed a little the code provided by noiasca to show you which part is failing to compile.
Hope it will give you better understanding.
The main issue/error is :
error: conflicting return type specified for 'virtual float Sensor2::myOtherMethod(int)'
float myOtherMethod(int);
Because the overriding method in derived class use another return type than the base class.
Searching by myself let me beleive that i have to use pointer for function return to "abstract" this return. But i do not like it (maybe because i do not fully confident with pointer/syntax). What about that ?
Is there a "simple" or better way to address this issue ?
Ok if it does not work. There always a reason.
But what are the options here.
To give more background and understand the goals :
Sensors share a lot of common (ie commands sent by uart, calibration).
Sensors return results as an int and some kinds require some more calculation (ie to get values in %, and adjust precision with decimals, some can send result in a specific unit) and should return int or float or char*.
My design was to embark all the common stuff in the base class and add specific sensors calculation in the child class. It seems good for me, but my bad knowledge of C++ lead me to some issues. like the one reported.
The actual code have in the base class the standard calculation method and the derived class is running the base method, apply calculations and return the result in another type. Because next sensor generation will behave deferently i choose this (bad?) desing.
As a result, discussions and explanations it leads me to this questions :
is my design correct or must be re-evaluated ?
should i adapt the existing code ?
should i rewrite the existing code because it will not be acheived like i wrote it ?
Maybe some experienced developer can give me some ideas/answers.
when you have a different return type / a different signature - you don't need a late binding and therefore you don't need to override / virtual.
Regarding your usecase:
when you have different sensors (with different return types) I see no need for a "return result" member function in the base class at all. If you have a "common" value calculation, name it what it is ... some kind of calculation.
Some of the posts seem to confuse overloading (two methods with different signatures and the same name, statically resolved) and overriding (a subclass method that hides a superclass method of the same name and same signature dynamically at runtime).
The C++ word for superclass is base class and for subclass is derived class, the normal terms used everywhere else!