Passing structs to functions
Adam D. Ruppe via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jul 2 13:40:00 PDT 2016
On Saturday, 2 July 2016 at 20:24:12 UTC, phant0m wrote:
> I took a simple task (for D learning purposes): to implement a
> Point template "class" (something like Qt's QPoint).
Using ref is wasteful there regardless.... just take an ordinary
Point (even const is optional if it is all value but it doesn't
hurt).
I think a lot of C++ programmers overuse references. If you're
passing a large thing, it makes sense, but for small things it
often has an overall negative effect on performance, but I see
people saying they ALWAYS use struct& just out of habit.
Even if you have an array slice or class object in there in D,
they are already references, so you don't want to use ref again
on top of it. Most the time, you're probably ok just passing a
normal T or const T.
> As far as I want to be able to add two points, I've implemented
> opAdd() function:
opAdd is the old way in D, nowadays it is more like
Point opBinary(string op : "+")(const Point other) const {
return Point(x + other.x, y + other.y);
}
More information about the Digitalmars-d-learn
mailing list