"Effective C + +" clause 20 ning with Pass-by-reference-to-const replacement Pass-by-value

Source: Internet
Author: User

"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 :

    1. 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.

    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.