+ 1
Why operator+(MyClass &obj) rather than operator+(MyClass obj)?
It seems that operator+(MyClass &obj) and operator+(MyClass obj) give the same output 67. Does this mean the two work in the same way?
12 odpowiedzi
+ 4
~ swim ~
const int* pint ...
int* const pint ...
const MyClass& obj ...
MyClass const& obj ...
Those are all the same?
+ 3
http://www.cplusplus.com/doc/tutorial/functions/
scroll down to:-
"Efficiency considerations and const references."
Although it uses "strings" in the example, the explanation is still relevant (and why/when to use a const reference).
+ 3
That's something a lot of programmers do to avoid copying. If MyClass was a class with like 100 member variables, it would take a while to copy all those in a new MyClass object. So instead you create a reference to it which is instant. It is prefered to be constant so you make sure that you won't change it (if you don't want to) So it would be:
operator+(const MyClass&obj)
+ 2
~ swim ~
It may be just the right time, and an added point to explain the difference between this;
const <type>& something
And this;
<type> const& something
Usage preferences and best practices of each would be a bonus for learners 👍
+ 2
~ swim ~
I recall you once told me they are different. So I thought maybe share for a wider scope.
Alright then, no problem 👌
+ 2
~ swim ~
IIRC we were talking about the `const` position, and in regards to reference, not about pointers.
+ 2
~ swim ~
Okay I read that post ...
Thank you 🙏