Negative tive C ++ Clause 20: replace pass-by-reference-to-const with pass-by-value

Source: Internet
Author: User

Key points:

  • Try to replace pass-by-reference-to-const with pass-by-value. This is usually more efficient and avoids the problem of object cutting when the subclass object is copied to the base class.
  • Pass-by-value should be used for the internal value type, STL iterator, and function object.

 

By default, c ++ uses the by value Method to pass the object (or from) function. The function parameter is the initial value of the copy of the real parameter. A copy obtained by the function is also the return value of the function. These copies are produced by the copy constructor of the object, this may make pass-by-value an expensive operation:

class Person { public:     Person();     virtual ~Person(); protected: private:     std::string name;     std::string address; };class Student : public Person{ public:     Student();     ~Student(); private:     std::string schoolName;     std::string schoolAddress; };bool validateStudent(Student s); Student plato; bool platoIsOK = validateStudent(plato);

When the above function calls validateStudent (plato);, Student's copy constructor is called and s is initialized Based on plato. When validateStudent returns s, it is destroyed. Therefore, the parameter transfer cost is "one Student copy constructor call and one Student destructor call ".

But it is not the whole story. The Student object has two string objects, and the Student object inherits from the Person object. Therefore, the Person object must be constructed, and the Person object has two strings in it.

Therefore, passing a Student object in the by-value method will result in one Student copy constructor, one Person copy constructor, and four string copy constructor calls. When the Student replica is destroyed, each constructor action requires a corresponding destructor call action. Therefore, the overall cost is "Six constructor and six destructor "!

 

The method to avoid these constructor and const is pass-by-reference-to-const:

bool validateStudent(const Student& s);

In this way, no constructor or destructor is called because no new object is created. Const is necessary, and callers do not have to worry about validateStudent changing the Student they passed in.

 

 

By referenceYou can also avoidSlicing-free (Object cutting) Problems. When a derived class object is passed to a base class object by value, the copy constructor of the base class is called, the special properties of "constructing this object is like a derived class Object" are cut out, leaving only one base class object.

 

class Window { public:     std::string name() const;     virtual void display() const; };class WindowWithScrollBars : public Window { public:     virtual void display() const; };void printNameAndDisplay(Window w) {     std::cout << w.name();     w.display(); }WindowWithScrollBars wwsb; printNameAndDisplay(wwsb);

W will be constructed into a Window object: pass-by-value, resulting in the removal of all the special information of wwsb "the reason why it is a wide wwithscrollbars object. Therefore, display always calls Window: display ().

The solution to cutting is to pass w by reference to const:

 

void printNameAndDisplay(const Window& w) {     std::cout << w.name();     w.display(); }

 

 

When you look at the bottom layer of the c ++ compiler, you will find that the reference is often implemented by pointers. Therefore, the pass-by-reference usually means that the pointer is passed. Therefore, if you have an object of built-in type (such as int), pass by value is more efficient than pass by reference. This advice also applies to stl iterators and function objects, so they are traditionally designed as pass by values. Practitioners have the responsibility to see if they are efficient and not affected by cutting issues.

 

 

 

 

 

 

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.