In the first example f()
obtains a copy of the original object and hence cannot possibly change the latter. However, the copy constructor is invoked, which may be quite expensive and is therefore not advisable as a general method.
In the second example f()
either obtains a pointer or (for f(obj&x)
) a reference to the original object and is allowed to modify it. If the function only takes a const
pointer or reference, as in f(const object&x)
, it cannot legally change the object. Here, no copy is made. Therefore, passing by const reference is the standard approach for parameter that shall not be modified.