|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D.gnu - D noob question
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: Aug 21 2005
|