Effective C + + reading notes clause 20

Source: Internet
Author: User

Clause 20: Prefer to replace pass-by-value with Pass-by-reference-to-const

1: The latter is highly efficient; look at the code:

#include <iostream>using namespace Std;class person {public:person () {cout<< ' person () ' <<endl;} Person (const person& p) {cout<< ' person (const person& p) ' <<endl;} Virtual ~person () {cout<< "~person ()" &LT;&LT;ENDL;} Private:string name;string address;}; Class Student:public person{public:student () {cout<< "Student ()" &LT;&LT;ENDL;} Student (const student& p) {cout<< "Student (const student& p)" &LT;&LT;ENDL;} Virtual ~student () {cout<< "~student ()" &LT;&LT;ENDL;} Private:string schoolname;string schooladdress;}; void Print (Student s) {}int main () {Student stu; Print (Stu); return 0;} /*student Stu; Print (Stu); When the above function is called, the copy constructor of student is called, and S is initialized with Stu as a blueprint, and S is destroyed at the end of the function, so the cost of passing the parameter is a student copy constructor call. Add a student destructor call person has two string objects student has two string objects so the cost of passing the above parameter is to add 4 string constructor and destructor, once student constructor call, The constructor call of the person must be this way down, the cost of the above is the total: six constructors and six destructors effective C + + clause 20 says this will call the copy constructor of student and the copy constructor of person, and here I experiment to invoke St The copy constructor of the udent, but does not invoke thecopy constructor; I wrote a program to prove I was right, calling the copy constructor of a subclass, and not calling the copy constructor of the parent class. In conclusion, the efficiency of such transmission is too low. */

I've written a program to prove that I'm right, calling the copy constructor of a subclass, and not calling the copy constructor of the parent class.

Look at the code:

#include <iostream>using namespace Std;class parent{public:parent () {cout<< "Call the parameterless constructor of the parent class" <<ENDL; Parent (const parent& p) {cout<< "copy constructor of parent class" <<ENDL; ~parent () {}};class children:public parent{public:children () {cout<< "Call the parameterless constructor of the subclass" <<ENDL; Children (const children& C) {cout<< "copy constructor of subclass" <<endl;} ~children () {}};int main () {children AA; Children BB (AA);/* Call the parameterless constructor of the parent class to call the non-parametric constructor of the subclass to call the copy constructor of the parent class's parameterless constructor subclass */return 0;}


The code above illustrates that when a subclass object calls the copy constructor to construct an object, it does not call the copy constructor of the parent class, but instead calls the normal constructor of the parent class.

2: The latter avoids the object cutting problem; Look at the code:

#include <iostream>using namespace Std;class base{public:base () {cout<< "Call the constructor of the parent class" &LT;&LT;ENDL; Base (const base& B) {cout<< "calls the copy constructor of the parent class" &LT;&LT;ENDL; ~base () {}virtual void display () const{cout<< "calls the display function of the parent class" &LT;&LT;ENDL; Private:int i;}; Class Derived:public base{public:derived () {cout<< "calls the constructor of the subclass" &LT;&LT;ENDL;} ~derived () {}virtual void display () const{cout<< "calls the display function of a subclass" <<endl;}}; void print (Base b) {b.display ();//parameter is cut even if the child class object is passed, the call is also the display function of the parent class}void Print2 (const base& b) {b.display ();} int main () {Derived aa;print (AA);//Call the copy constructor of the parent class the print function of the parent class; Print2 (AA); return 0;}  /* Passing in a value pass one of the drawbacks mentioned above is: inefficiency. Another drawback: object cutting problem void print (Base b) {b.display ();//parameter being cut calls the function of the parent class} Derived AA;  Print (AA); The AA is a subclass, and the print parameter is the base class when the object of a subclass is passed as a value and is treated as a base class object, the copy constructor of the base class is called, and the attributes that cause this object to behave like a subclass object are completely cut off, leaving only a base class object.  A parametric cut has occurred. Using reference values can solve this problem. */

So when a subclass is assigned to a parent class in a value-passing way, the object is cut and the reference is substituted for him.

To summarize a sentence:

Replace Pass-by-value with Pass-by-reference-to-const as much as possible, while the former is usually more efficient and avoids cutting problems.


Effective C + + reading notes clause 20

Related Article

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.