<<effective c++>> Reading notes---clause 20: better to replace Pass-by-value with Pass-by-reference-to-const

Source: Internet
Author: User
  1. Replace Pass-by-value with Pass-by-reference-to-const as much as possible. The former is usually more efficient because it avoids the call to copy constructors and destructors, and avoids cutting problems.
    is a good example of slicing problems:
Class A {public:a () {}virtual ~a () {}virtual void Display () {cout << "A::D isplay ()" << Endl;}; Class B:public A{public:b () {}virtual ~b () {}virtual void Display () {cout << "b:display ()" << Endl;}; void Show (A a) {A.display ();} int _tmain (int argc, _tchar* argv[]) {b b; Show (b); return 0;}

Output:

2. If you spy on the bottom of the C + + compiler, you will find that references is often implemented as pointers, so pass-by-reference often means that pointers are really passed, so 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.

<<effective c++>> Reading notes---clause 20: better to replace Pass-by-value with Pass-by-reference-to-const

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.