I have a base and a derived class:
plugin.h
#ifndef PLUGIN_H
#define PLUGIN_H
// plugin states
#define PLUGIN_IDLE 0
class Plugin {
public:
Plugin();
~Plugin();
virtual void loop();
};
#endif
plugin.cpp
#include <Arduino.h>
#include "plugin.h"
Plugin::Plugin() {
}
Plugin::~Plugin(){
}
void Plugin::loop(){
Serial.println("Plugin::loop()");
}
derived class
class OneWirePlugin : public Plugin {
public:
OneWirePlugin(byte pin);
void loop();
};
OneWirePlugin::OneWirePlugin(byte pin) {
}
void OneWirePlugin::loop() {
Serial.println("OneWirePlugin::loop()");
}
I was expecting that calling the derived instance's loop()
method would execute OneWirePlugin::loop()
.
However, this only happens when I call it in derived class context:
Plugin p = Plugin();
Plugin o = OneWirePlugin(ONEWIRE_PIN);
OneWirePlugin q = OneWirePlugin(ONEWIRE_PIN);
p.loop(); // Plugin::loop()
o.loop(); // Plugin::loop()
q.loop(); // OneWirePlugin::loop()
What's wrong with my virtual method that will allow be calling the derived implementation, especially when referenced via *Plugin
pointers?