Effective C + +: regulation 20: ning Pass-by-reference-to-const Replacement Pass-by-value

Source: Internet
Author: User

A

Assuming that the parameter is passed when the function is called pass-by-value, then the function's argument is based on the actual parameter of the copy of the original value, called, and also gets a copy of the end value returned by the function.

Take a look at the following code:

Class Person {public: Person     ();     Virtual ~person ();  Private:     string name;     string address; };class student:public Person {public:     Student ();     ~student (); Private:     string schoolname;     String schooladdress; };bool validatestudent (Student s); Student Plato; BOOL Platoisok = Validatestudent (Plato);

When the above function calls Validatestudent (Plato), the copy constructor of student is called, and the S is initialized as a blueprint for Plato. When Validatestudent returns S is destroyed. So the delivery cost of the parameter is "one student copy constructor call, one student destructor call".

But it's not the whole story. There are two string objects within the student object, 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 By-value mode causes the student copy constructor, the person copy constructor, and the four string copy constructor to be called at a time. When that student copy is destroyed. Each constructor action requires a corresponding destructor call action. So the overall cost is "six-time constructor and six-time destructor"!

Workaround: Pass-by-reference-to-const

BOOL Validatestudent (const student& s);
such a way does not matter what constructs and destructors are called. No matter what new objects are created. The const is necessary. Callers don't have to worry about validatestudent change the student they're passing in.


Two

The by reference can also avoid slicing (object segmentation) problems. When a derived class object is passed to a base class object with by value.

The copy constructor of the base class is called. The special properties of the "construct this object behave like a derived class object" are split, leaving only a base class object. Because it is the constructor of the base class, it is created.

If you have the following inheritance relationships:

Class Window {public:     string name () const;     virtual void display () const; };class windowwithscrollbars:public Window {public:     virtual void display () const;};
Incorrect function notation:

void Printnameanddisplay (Window w) {     std::cout << w.name ();     W.display (); }
When the above function is called:

Windowwithscrollbars WWSB; Printnameanddisplay (WWSB);
W will be constructed as a Window object: He is pass-by-value, causing the WWSB "to be a Windowwithscrollbars object" all the special information is removed. So the display calls always window::d isplay (). This is definitely not what we want!

!!

Workaround:
To pass W by the reference to Const method:

void Printnameanddisplay (const window& w) {     cout << w.name ();     W.display (); }
Now, what type of form is passed in. W shows that type.


Three
Some compilers treat "built-in types" and "user-defined types" differently, even if they have the same underlying representation. Some compilers put a double variable in the cache. However, a class containing only a double is not put into the buffer.

peek at the bottom of the C + + compiler. You will find. Reference are often implemented as pointers, so pass-by-reference often means that pointers are passed. So suppose you have an object that belongs to a built-in type (such as int) pass by value is often more efficient than pass by reference. This advice also applies to STL iterators and Function Objects . They are therefore customarily designed as pass by value. Practitioners have a responsibility to see if they are efficient and unaffected by segmentation issues.


Please remember:
(1) try to Replace Pass-by-value with Pass-by-reference-to-const. The former is usually more efficient and avoids segmentation problems (slicing  problem).
(2) The above rules are not appropriate for the built-in type. and STL iterators and function objects.

For them, pass-by-value tend to be more appropriate.





Effective C + +: regulation 20: Ning Pass-by-reference-to-const replace Pass-by-value

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.