Reputation: 313
I want to ask one simple question which is making me confuse. for example if I write in argument there is no reference &. but in the second case I have used & with rectangleType&, I am having confusion why we use this & when we can do it without this &. Where is this necessary and why we use it. My question is not about copy constructor.
rectangleType rectangleType::operator+
(const rectangleType rectangle)
Case 2:
rectangleType rectangleType::operator+
(const rectangleType& rectangle)
Upvotes: 1
Views: 81
Reputation: 24885
In the first method, it will receive a (complete) instance of rectangleType
, that will be created using the copy constructor just to be passed as a parameter.
In the second method, it will receive a reference to an already existing instance; no copy will be made.
Upvotes: 0
Reputation: 17250
When you pass an object like rectangleType
(or any other type) by value, you're making a copy of that object. This means:
So, pass by reference whenever:
Pass by const reference whenever you don't need the function to be able to modify the original object.
For user-defined types, this essentially boils down to "pass by reference wherever possible."
Upvotes: 2
Reputation: 11482
How can you state that your question is not about copy construction if the answere is about it?
So to conclude: You get benefits without disadvantages.
Upvotes: 0