still confused about call by reference
Derek Parnell
derek at nomail.afraid.org
Sun Oct 28 22:37:31 PDT 2007
On Mon, 29 Oct 2007 04:32:44 +0100, Hoenir wrote:
> I'm still a bit confused about call by reference. When do I have to
> explicitly use the & operator and when to use in, out and inout?
> I want to convert my C++ vector struct to D:
>
> struct vec3
> {
> vec3 operator+(const vec3& v) const
> {return vec3(x+v.x, y+v.y, z+v.z);}
> vec3 operator-(const vec3& v) const
> {return vec3(x-v.x, y-v.y, z-v.z);}
>
> I'm also wondering about how to handle the constness.
> Thanks in advance for any help :)
I'm no C++ user so I'm guessing a bit with the C++ syntax, but I'd code
something like ...
module vecs;
struct vec3(VT)
{
private VT x,y,z;
vec3 opAdd(const ref vec3 v)
{
vec3 t;
t.x = x + v.x;
t.y = y + v.y;
t.z = z + v.z;
return t;
}
vec3 opSub(const ref vec3 v)
{
vec3 t;
t.x = x - v.x;
t.y = y - v.y;
t.z = z - v.z;
return t;
}
void opCall(T,U,V)(const T a, const U b, const V c)
{
x = cast(VT)a;
y = cast(VT)b;
z = cast(VT)c;
}
private import std.string;
string toString()
{
return std.string.format("[%s; %s; %s]", x,y,z);
}
}
**** EXCEPT **** that 'const ref' crashes the compiler (see Bugzilla 1319)
http://d.puremagic.com/issues/show_bug.cgi?id=1319
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
29/10/2007 2:58:45 PM
More information about the Digitalmars-d-learn
mailing list