Can anyone explain why the following does not work?
class Base {
protected:
Base(){};
bool tgl(uint8_t);
};
bool Base::tgl(uint8_t _id) {
static bool _toggle[6];
_toggle[_id] = (_toggle[_id] ? 0 : 1);
return _toggle[_id];
}
class Derived: public Base {
Derived() {};
public:
static Derived& initialize();
};
Derived& Derived::initialize() {
static Derived _instance;
return _instance;
}
Derived& derived = Derived::initialize();
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(derived.tgl(5)); // Error within this context bool Base::tgl(uint8_t) is protected
delay(1000);
}
Making Base::tgl(uint8_t) a public member does what I want, it simply outputs alternating 1's and 0's to the serial monitor. But the fact that it works just confuses me further, because I am calling the protected constructor Base() when I create an instance of Derived with no problems. So why can't I can't call the protected member function tgl() from my derived instance, if that member is indeed protected?