[020] Ning replaces pass-by-value with Pass-by-reference-to-const

Source: Internet
Author: User

Preface:

We've all used the value-passing method of C, so how does the value transfer work in C + + cases?

Like what:

int foo (int  x); int I;foo (i);

1. Get a copy of I within the program

2. Passing a copy to the Foo function

3.foo back

These replicas are produced by copy constructors, which can cause values to be passed into a cumbersome operation when there are too many arguments or complex logic.

Section one: replacing Pass-by-value with Pass-by-reference-to-const

Let's look at the value-passing process of the class:

1 classA {2  Public:3A () {cout <<"Call a\n"; }4AConsta& acopy) {//copy Constructor5A =acopy.a;6     }7     Virtual~A () {}8 Private:9     stringA;Ten }; One  A classB: PublicA { -  Public: -B () {cout <<"Call b\n"; } the~B () {} - Private: -     stringb; - }; +  - voidFoo (b b) { +     return; A}

Next we call this:

B B1;                  // will call A's constructor                        directly first // then call the constructor of B Foo (B1);               // replica calls A's constructor                        // call the constructor of B again

The above is not all data called, because we also have a string member of the Class!

The constructor of string is called each time it is constructed, so in addition to calling the constructor of a once and the constructor of the B once, we also call the constructor of string two times.

In addition, we need to call the corresponding destructor, so the total number of code calls above is: 4 constructors + 4 destructors.

Simple one assignment, resulting in so many function calls, efficiency is the problem!

Scenario: We can use the const reference to solve this problem. In this way, no constructors or destructors need to be called.

void foo (const b& B) {    return;}

Because the reference is directly manipulating the object, there is no copy of the saying. In addition, it is defined as const, in order to not let the passed arguments be changed incorrectly!

In addition to reducing the number of call functions, using a const reference can also provide the following benefits.

★ Avoid Object cutting problem (slicing)

Use an example of an image to explain:

1 classA {2  Public:3A () {cout <<"Call a\n"; }4AConsta&acopy) {5A =acopy.a;6     }7     Virtual~A () {}8 Private:9     stringA;Ten }; One  A classB: PublicA { -  Public: -B () {cout <<"Call b\n"; } the~B () {} - Private: -     stringb; - }; +  - voidFoo1 (A a) { +     return; A}

If we pass the subclass object to the Foo1 () function at this time.

B B1;foo1 (B1);    Child to Father

At this time, B1 is considered a base class object when the value is passed, thus producing a copy of the base class, and a copy constructor is called, so the attributes in sub-Class B are all cut, leaving only a base class Object!

The best way to solve the cutting problem is to use the const reference to pass the value!

void foo1 (const a& a) {  //  no longer calls a copy constructor!     return;}

section II: Delivery of basic types

If we go to the bottom of the C + + compiler, we will find that the reference is often implemented as pointers, so passing through the pointer usually means that the pointer is actually passed.

Therefore, for built-in types, the efficiency of value passing is actually more efficient than reference delivery.

For STL iterators and function objects, this result is also applicable.

Summary

1. Replace Pass-by-value with Pass-by-reference-to-const as much as possible. The former is usually more efficient and avoids cutting problems (slicing problem);

2. When it comes to built-in types, as well as STL iterators and function objects, it is generally more efficient to pass values.

[020] Ning replaces 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.