Hi there, not sure if this is an Arduino specific thing or just C++, but I thought I'd ask here in case.
I'm trying to learn how to overload the << operator so if I create a class (say called fred) I can then say:
Serial << fred << endl
rather than:
Serial << fred.get() << endl
I've Googled like crazy and tried the non-arduino examples with no success. I expect there is a library I could use as an example. Can anyone point me in a good direction or offer help? I've also looked at Print.h and Printable.h, but I'm still learning this C++!
The base of the code I'm playing around with is below.
Thanks, Glyn
#include <streaming.h>
class StTest {
private:
int stash ;
public:
StTest();
void set(int a);
int get();
friend Print &operator<<( Print &output, const StTest &D )
{
//output << stash << endl;
return output;
}
};
StTest::StTest() { stash = 0; }
void StTest::set(int i) { stash = i; }
int StTest::get() { return stash; }
void setup()
{
/* add setup code here */
Serial.begin(115200);
StTest * cl = new StTest();
cl->set(5677);
Serial << cl->get() << endl;
//Serial << cl << endl;
}
void loop()
{
/* add main program code here */
}