Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 2 months ago.
Can I use virtual functions to create a sensor class
Hi, I'm trying to make a sensor class with derived classes for thermistors, digital thermometers, analog thermometers , LDRs and some pressure sensors. I would like to use an array of the base sensor class so it's easy to use a for/next loop to read all the sensors. Before I start to write the library and burn up the few neurones I have left does any one know if GNU supports this? Cheers Mike
2 Answers
10 years, 2 months ago.
Yes, that will work fine. Anywhere that you have a type of the base pointer you can substitute a derived class.
Just keep in mind that since you'll have pointers to the base class you'll only be able to call functions defined in the base class, in order to call any functions that are specific to a derived class you will need to cast the pointer to the correct class.
e.g.
class sensor { ... }; class thermistor : sensor { ... }; sensor *sensorArray[5]; sensorArray[0] = new thermistor(); sensor *nextSensor = sensorArray[0]; // This would give an error thermistor *thermSensor = sensorArray[0]; // If you want to access sensor class specific functions you would instead need to // define a getType() method and have a list of return values and then use code like this: if (sensorArray[0]->GetType() == thermistorID) thermistor *thermSensor = (thermistor *)sensorArray[0];
Generally it's best to have just about any method you will want for any sensor defined in the base class as a virtual function with the base class version not actually doing anything. That way you don't need to worry about casting to the correct pointer.
One thing that is a good tactic in this situation is to make sure you have at least one pure virtual function in the base class, something that everything will need to implement like getting the value e.g.
class sensor() { public: virtual float getReading() = 0; ... ... }
By making the function a pure virtual (the = 0 bit at the end) you will cause the compiler to throw an error if someone tried to create an item of the base class directly or if one of the derived classes forgets to define it. Making it so that it complains if you try to compile something silly is far easier to than trying to track down what's going on if it does compile and then has issues.
Thanks for your input (pun intended) and I found out that Ladyada (https://learn.adafruit.com/using-the-adafruit-unified-sensor-driver) has done some work on this already. I'll look over her code, which I'm sure will be better than anything I could write, and see if I can port it to mbed. Cheers Mike
posted by 20 Sep 201410 years, 2 months ago.
Not 100% sure, but if you just instantiate your classes the normal way, and additionally have an array of base class pointers, I think it works if you fill that array with pointers to the constructed objects.