pass by reference or by value when necessary#228
Conversation
| } | ||
|
|
||
| void ContourPlot::selected(const QPointF& pos){ | ||
| void ContourPlot::selected(QPointF pos){ |
There was a problem hiding this comment.
I get that you are adding tons of "const &" and that makes it so it uses the incoming variable as is but promises not to modify it.
But why are you removing the const & in a few cases such as this one?
There was a problem hiding this comment.
Basically passing large classes by value makes the compiler do a copy of the variable on the stack. That's why you pass by reference with & (I'm a C expert so I see this more or less like pass by pointer). Adding const tells compiler and reviewer that the variable will not be modified inside the function.
You don't want to pass an int by pointer or reference because it adds unnecessary operation to make the pointer or reference. Small variables are already in registers and don't need copy on stack.
QPointF is a class but in fact it's really just 2 values. So it's small enough to prefer passing by value.
Now I understand this one (QPointF) is really misleading and I don't think it changes anything to the code. I would almost lean to let it like it was with pass by reference. It's just a little more work as those chances were automated.
gr5
left a comment
There was a problem hiding this comment.
These changes look extremely safe and a good improvement.
I have used clazy to automatically use refs and values where it should.
QPointis misleading but it's indeed a simple type to pass by value.Notice there is no big risk changing
type atoconst type &aand reverse.Those changes should bring a marginal efficiency gain. Nothing really measurable.