Reputation: 93
How do I implement my own custom stream in C++?
Why?
I want to send data from one micro-controller to another using a wired connection and I think a custom stream is the most intuitive way.
Example:
#include "myStream.h"
int main()
{
myStream << "Hello world!";
return 0;
}
EDIT:
Solution:
class Stream
{
private:
// members
public:
Stream() {}
friend Stream& operator<<(Stream& stream, const Whatever& other);
};
Stream& operator<<(Stream& stream, const Whatever& other)
{
// do something
return stream;
}
Upvotes: 0
Views: 272
Reputation: 211580
If you look at how streams work, it's just a case of overloading operator<<
for both your stream object, and the various things you want to send to it. There's nothing special about <<
, it just reads nicely, but you could use +
or whatever else you want.
Upvotes: 2