D.gnu - D noob question
- John Love-Jensen (68/68) Aug 21 2005 Hi everyone,
- Artem Gr (2/13) Aug 21 2005
- Derek Parnell (79/162) Aug 21 2005 I'm not really a C++ coder so I had to make a few guesses as to what you
- John Love-Jensen (6/6) Aug 22 2005 Thanks Artem, I did not know about that other forum. :-)
Hi everyone, I'm new to D Programming Language. I was wondering how one would code the following (see below) C++ code in D. Some of the paradigms in particular: + using the assignment operator as the setter + using the cast operator as the getter + working with the output system to print the object's state This part in D is easy... typedef int X; typedef int Y; ...I wish C++ had actually typing typedef instead of mere aliasing typedef. The assignment-setter paradigm and the cast-getter paradigm are less clear to me for how to do them in D. I have no clue how to incorporate Point with import std.stdio's writef. Thanks, --Eljay - - - - - - - - - - - - - - - - - - - #include <iostream> using std::cout; using std::endl; using std::ostream; class X { int m; public: explicit X(int in) : m(in) { } X() : m(0) { } operator int () const { return m; } }; class Y { int m; public: Y() : m(0) { } explicit Y(int in) : m(in) { } operator int () const { return m; } }; class Point { X x; Y y; public: Point(X inX, Y inY) : x(inX), y(inY) { } Point() { } void operator = (X inX) { x = inX; } void operator = (Y inY) { y = inY; } operator X () const { return x; } operator Y () const { return y; } }; ostream& operator << (ostream& output, Point const& point) { output << X(point) << "," << Y(point); return output; } int main() { Point point; X x; Y y; x = X(17); cout << "point: " << point << "\nx : " << x << "\ny : " << y << endl; point = x; point = Y(33); y = point; cout << "point: " << point << "\nx : " << x << "\ny : " << y << endl; }
Aug 21 2005
This question is probably more suitable for "digitalmars.D.learn". http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.learnHi everyone, I'm new to D Programming Language. I was wondering how one would code the following (see below) C++ code in D. Some of the paradigms in particular: + using the assignment operator as the setter + using the cast operator as the getter + working with the output system to print the object's state ...
Aug 21 2005
On Sun, 21 Aug 2005 14:29:43 -0500, John Love-Jensen wrote:Hi everyone, I'm new to D Programming Language. I was wondering how one would code the following (see below) C++ code in D. Some of the paradigms in particular: + using the assignment operator as the setter + using the cast operator as the getter + working with the output system to print the object's state This part in D is easy... typedef int X; typedef int Y; ...I wish C++ had actually typing typedef instead of mere aliasing typedef. The assignment-setter paradigm and the cast-getter paradigm are less clear to me for how to do them in D. I have no clue how to incorporate Point with import std.stdio's writef. Thanks, --Eljay - - - - - - - - - - - - - - - - - - - #include <iostream> using std::cout; using std::endl; using std::ostream; class X { int m; public: explicit X(int in) : m(in) { } X() : m(0) { } operator int () const { return m; } }; class Y { int m; public: Y() : m(0) { } explicit Y(int in) : m(in) { } operator int () const { return m; } }; class Point { X x; Y y; public: Point(X inX, Y inY) : x(inX), y(inY) { } Point() { } void operator = (X inX) { x = inX; } void operator = (Y inY) { y = inY; } operator X () const { return x; } operator Y () const { return y; } }; ostream& operator << (ostream& output, Point const& point) { output << X(point) << "," << Y(point); return output; } int main() { Point point; X x; Y y; x = X(17); cout << "point: " << point << "\nx : " << x << "\ny : " << y << endl; point = x; point = Y(33); y = point; cout << "point: " << point << "\nx : " << x << "\ny : " << y << endl; }I'm not really a C++ coder so I had to make a few guesses as to what you expected as the output. Anyhow, here is an attempt to code this in D ... //-------------------------- import std.stdio; class X { private{ int m; } // Constructors this(int in_val) { m = in_val;} this() { m = 0; } // Convert this object into a string form. char[] toString() { return std.string.format("%d", m); } } class Y { private{ int m; } // Constructors this(int in_val) { m = in_val;} this() { m = 0; } // Convert this object into a string form. char[] toString() { return std.string.format("%d", m); } } class Point { private{ X _x; Y _y; } // Constructors this(X inX, Y inY){ _x = inX; _y = inY; } this() { _x = new X; _y = new Y;} // Define some properties... void x(X inX) { _x = inX; } // setter X x() { return _x; } // getter void y(Y inY) { _y = inY; } // setter Y y() { return _y; } // getter // Convert this object into a string form. char[] toString() { return _x.toString ~ "," ~ _y.toString; } } int main() { Point point = new Point; X x = new X; Y y = new Y; x = new X(17); writefln("point: " , point.toString, "\nx : " , x.toString , "\ny : " , y.toString); point.x = x; point.y = new Y(33); y = point.y; writefln("point: " , point.toString, "\nx : " , x.toString , "\ny : " , y.toString); return 0; } //-------------------------- This program produces the output ... point: 0,0 x : 17 y : 0 point: 17,33 x : 17 y : 33 -------------------- The main differences are that you need to explicitly do a 'new' for objects otherwise they are not created. There is no way to override the '=' operator in D, but there are 'properties' which replace a lot of uses for '=' overriding. And 'ostream' stuff is generally handled by the toString member function, though theoretically you could override the '<<' operator but that is not the D way. -- Derek (skype: derek.j.parnell) Melbourne, Australia 22/08/2005 10:01:47 AM
Aug 21 2005
Thanks Artem, I did not know about that other forum. :-) Thanks Derek, that was exactly the kind of "how to" example I was looking for. Helps me understand how the concepts are implemented in D vs C++. Sincerely, --Eljay
Aug 22 2005