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

Source: Internet
Author: User

1. Advantages of passing parameters with Reference-to-const:

Avoid the construction and destruction of local objects, save time and space;

Avoid "object cutting" when the parameter-derived class object is passed to the base class object, and implement object-oriented;

2. Although reference is an "alias", the underlying implementation is often a pointer, so for built-in types, the choice of pass-by-value is more efficient. This also applies to STL iterators and function objects, because they are traditionally designed to be pass-by-value, But not the object of a small choice pass-by-value efficiency is high, mainly for the following reasons:

object contains pointers to heap memory;

Some compilers put built-in types in the cache, but do not put objects containing only one built-in type into the cache (the object's reference is a built-in type);

User-defined type its internal implementation may change (objects may become larger)

3. In general, the use of Pass-by-value to compare the appropriate object is generally only built-in type, STL iterator and function object, the other best replaced with Pass-b0reference-to-const pass-by-value

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.