Passing structs to functions
phant0m via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 2 13:24:12 PDT 2016
On Saturday, 2 July 2016 at 19:46:53 UTC, Adam D. Ruppe wrote:
> On Saturday, 2 July 2016 at 18:37:06 UTC, phant0m wrote:
>> How should I pass a struct variable in D effectively?
>
> Passing by value is often the most efficient. It depends on
> what exactly you have in the struct.
From the point of view of a hardcore C++ programmer, D looks very
promising and very strange at the same time. I understand that my
C++ habits are not applicable here, so I'm trying to find a
"correct" way to do basic things.
I took a simple task (for D learning purposes): to implement a
Point template "class" (something like Qt's QPoint).
As far as I want to be able to add two points, I've implemented
opAdd() function:
struct Point(T) {
static assert(__traits(isArithmetic, T));
T x;
T y;
Point opAdd(const ref Point other) const {
return Point(x + other.x, y + other.y);
}
}
Now, to be able to use rvalues, I need to add another one:
Point opAdd(const Point other) const {
return opAdd(other);
}
That's where my question came from. Of course, I can use just one
function, which accepts arguments by value. I'm just unsure
whether it's a good decision and a common practice.
More information about the Digitalmars-d-learn
mailing list