#ifndef mylib_h
#define mylib_h
#include "Arduino.h"
class Base {
public:
Base();
virtual ~Base();
virtual int read() ;
};
class Sub : Base {
public:
Sub();
virtual ~Sub();
int read();
};
#endif
setup
in loop
setup
in loop
setup
in loop
setup
in loop
setup
in loop
setup
I've tried a number of things:
defining Sub.read() as virtual in header file
not implementing Base.read() but instead setting it as "= 2" in header file.
removing Base.read() (works!), but then i have no common super class with a read() method.
not using pointers (works!), but then I cannot, or don't know how to, use a Base reference to invoke read() - which I want to do as there will be more subclasses with different read() implementations in the library I'm trying to create):
Whilst it is good to have a virtual destructor, I don't think you need a destructor at all unless you are planning to do something with it. I'm assuming this is a cut-down example.
Good question, for some reason I was assuming that the 'new' keyword was not working on Arduino (may have read that somewhere else), and I thought that &Sub() would create a Sub instance and give me its pointer (it does compile..).
I'll try your solution when i get home from work today, many thanks
I don't need to destroy any of my objects, so thanks for the tip
Yes, but your object really has to be created in global space then to be very useful, and you don't control the initialization order. Plus, what's the point? If you have the object at global space, why bother taking a pointer to it?
Yes, but your object really has to be created in global space then to be very useful, and you don't control the initialization order. Plus, what's the point? If you have the object at global space, why bother taking a pointer to it?
For example if you want to implement an Object Oriented state machine pattern:
// The following are derived from "State" class
StateA aObj;
StateB bObj;
StateC cObj;
State *currentState = &aObj;
// Update the state according to external events.
// currentState = currentState->ProcessInput();