re-creating C++'s reference bahavior
Derek Parnell
derek at nomail.afraid.org
Tue Apr 17 00:31:48 PDT 2007
On Mon, 16 Apr 2007 20:47:31 -0400, swiftcoder - Tristam MacDonald wrote:
> I am fairly new to D, from a C++ background, and I am having a
> hard time coming up with a sensible way to deal with what is
> a fairly trivial example in C++:
> ...
> But now in D, I find that the last statement requires ...
Does this below help any?
////////////////////
import std.stdio;
class Vector3f
{
int x;
float y;
}
class Node
{
Vector3f m_position; // (reference to) a Vector
Vector3f position() // Copy-getter
{
Vector3f t = new Vector3f;
// copy fields by hand
t.x = m_position.x;
t.y = m_position.y;
return t;
}
Vector3f* position_flds() // Reference-getter
{
return &m_position;
}
this() { m_position = new Vector3f; }
}
void main()
{
Node n = new Node;
writefln("A %s %s", n.position.x, n.position.y);
n.position_flds.x = 1;
n.position_flds.y = 2.2;
writefln("B %s %s", n.position.x, n.position.y);
Vector3f v = n.position;
writefln("C %s %s", v.x, v.y);
v.x = 10;
v.y = 9.9;
writefln("D %s %s", v.x, v.y);
writefln("E %s %s", n.position.x, n.position.y);
}
///////////////////
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
17/04/2007 5:30:04 PM
More information about the Digitalmars-d-learn
mailing list