C++ istream / ostream equivalent ?

Ali Çehreli acehreli at yahoo.com
Thu Dec 2 09:12:54 PST 2010


vincent picaud wrote:
 > Matthias Pleh Wrote:
 >> class A
 >> {
[...]
 >>      string toString()   { return someData;  }

 > I have the feeling that the C++ way is more convenient.
 >
 > Look in C++ , to define I/O for A, you do not have to modify
 > your class A and simply have to overload two functions:
 >
 > std::ostream& operator<<(std::ostream& out,const A& a)
 > std::istream&  operator>>(std::istream&  in,A&  a)

Yes but those two functions are actually logical parts of the class's 
interface. Besides, in many cases a 'friend' declaration is needed.

Tangentially, operator>> is overrated because it requires a complete 
object to work on. I find it exremely rare to input on top of an 
existing object, which would also require a default constructor; and 
being forced to write a default constructor is unpractical in some cases.

Reading from a stream should have been designed to return a constructed 
object:

     auto a = readFrom!A(some_input);

or as a static member function:

     auto a = A.readFrom(some_input);

Ali


More information about the Digitalmars-d-learn mailing list