toString() on struct template
Reiner Pope
reIGNOREiner.poCAPSpe at gmFOOail.cBARom
Wed Feb 28 13:01:37 PST 2007
Henning Hasemann wrote:
> I have a struct template with just 2 data members
> (thats why I'd actually preferred a struct over a class).
>
> It implements several methods such as overloaded operators
> and the pseudo constructor roughly like this:
>
> struct Vector2d(T) {
> T x, y;
> Vector2d!(T) opCall(T x, T y) {
> Vector2d!(T) v;
> v.x = x;
> v.y = y;
> return v;
> }
> char[] toString() {
> return format("(%d|%d)", x, y);
> }
> }
I couldn't get your code to work as is, because it wasn't a 'static'
opCall. The following code, however, runs fine on my computer:
> import std.stdio;
> import std.string;
>
> struct Vector2d(T) {
> T x, y;
> static Vector2d!(T) opCall(T x, T y) {
> Vector2d!(T) v;
> v.x = x;
> v.y = y;
> return v;
> }
> char[] toString() {
> return format("(%d|%d)", x, y);
> }
> }
>
> void main()
> {
> auto a = Vector2d!(int)(5, 6);
> writefln(a.toString());
> }
Hope that helps,
Reiner
More information about the Digitalmars-d
mailing list