"Effective C + +"
Clause 20: Prefer to replace pass-by-value with Pass-by-reference-to-const
By default, C + + passes an object to a function by value. Unless you know otherwise, the function parameter is the initial value of the copy of the actual parameter, and the call end is also a duplicate of the function return. These duplicate systems are produced by the copy constructor, which can make pass-by-value an expensive, time-consuming operation.
It is much more efficient to pass through Pass-by-reference-to-const: The reason is that no constructors or destructors are called because no new objects are created.
And by Pass-by-reference-to-const , you can avoid the problem of object cutting (slicing).
Note : If you have an object that belongs to a built-in type, pass by value is often more efficient than pass by reference to Const. So, in general, you can reasonably assume that the only object of "pass by value is not expensive" is the built-in types and STL iterators and function objects. As for everything else, please follow the advice of these terms and replace pass-by-value with Pass-by-reference-to-const as much as possible.
Summary :
Replace Pass-by-value with Pass-by-reference-to-const as much as possible. The former is usually more efficient and avoids the problem of object cutting.
The above rules do not apply to built-in types, as well as STL iterators and function objects. For them, pass-by-value tend to be more appropriate.
2016-11-07 20:11:04
This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1870391
"Effective C + +" clause 20 ning with Pass-by-reference-to-const replacement Pass-by-value