When I run Setup() I expect to see a ‘t’ in my console, followed by multiple ‘x’ characters. However it returns just multiple ‘t’ chars. It’s like my retrn never gets overwrited. Please see codesample below:
class Returner
{
public:
Returner(){}
char test()
{
}
};
class TReturner: public Returner
{
public:
TReturner(){}
char test()
{
return 't';
}
};
class XReturner: public Returner
{
public:
XReturner(){}
char test()
{
return 'x';
}
};
void setup()
{
Serial.begin(9600);
TReturner t = TReturner();
Returner * retrn = &t;
while(1)
{
Serial.print( retrn.test());
XReturner x = XReturner();
retrn = &x;
_delay_ms(500);
}
}
I can’t 100% explain that behaviour – I’d expect you wouldn’t get any characters printed as it’d be using Returner::test – but if you’re overriding a function in C++ you need to declare it virtual in the base class:
Without test being virtual, the line
(don’t you mean
retrn->test()?) will just pick one implementation oftestand use it always. As above I’d expect this to be the emptyReturner::test(). You may also either need to make Returner::test abstractor return some value if you’re leaving it with a function body.