Lazaro Caruso wrote:I've read that objects are always passed to functions as reference, and functions return reference to an object, so there is no difference if I write MyClass or MyClass&.
Are you sure that was about C++, or are you confusing it with another language such as
Java? Because for C++, that's not correct.
In Java, variables of non-primitive types are references. Not in C++: variables that are not pointers or references are just the value itself, which can be a complete object. You can pass complete objects as arguments and return them, as values. That's not possible in Java. Note that in Java the automatic memory management system keeps track of references to objects, and it makes sure the object is not discarded as long as there are any live threads that have references to it. C++ doesn't have automatic memory management, so it will not automatically keep objects around that you reference.
You should watch out with this in C++, because if the objects are large, passing them directly as values can be costly (the whole object will be copied when you pass it as a value).