C++ istream / ostream equivalent ?
Matthias Pleh
gonzo at web.net
Wed Dec 1 09:11:57 PST 2010
Am 01.12.2010 16:51, schrieb vincent picaud:
> Is there a canonical way to take into account a new type for I/O using the std phobos library ?
>
> To be clear I do not know how to translate something like this (in C++) in D :
>
> #include<iostream>
>
> class A {};
>
> std::ostream& operator<<(std::ostream& out,const A& a)
> {
> out<< "\nscreen output routine here\n";
> return out;
> }
>
> int main()
> {
> A a;
> std::cout<< a;
> }
>
> Do I have to overload some writeln functions ?
>
> any help is welcome :)
I would make it this way:
module test;
import std.stdio;
class A{}
class B{ string toString() {return "screen output routine here";}}
int main(string[] args)
{
A a=new A;
B b=new B;
writeln(a.toString());
writeln(b.toString());
return 0;
}
output:
> test.A
> screen output routine here
Base-class of all classes is object with have a default-toString()
method, which gives the module.classname.
So you can overwrite this method.
More information about the Digitalmars-d-learn
mailing list