I have two classes, A
and B
with the following public methods that write/read data to/from an ostream/istream:
class A {
void print( std::ostream & os = std::cout ) const ; // prints some data
};
class B {
void read( std::istream & is = std::cin ) ; // reads that data
};
After consulting this thread I used std::stringstreams
in the client code to pass data from A
to B
:
std::stringstream ss;
A a;
B b;
a.print(ss);
b.read(ss);
And it works pretty good. My question is: how could I further optimize this to avoid the copy overhead? I am looking something in the lines of a.read(b.print())
, which would naturally require B::print
to return a type inheriting both istream
and ostream
, but any viable solution which would skip the copy overhead would work.