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

Source: Internet
Author: User

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, and the result obtained by the function is also a copy of the function return value.

These replicas are produced by the object's copy constructor, which may make the 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 reference transmission parameters can also avoid slicing issues. When a derived class object is passed to a base class object by value,

The copy constructor of base class is called, and 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 required wwithscrollbars: public window
{
Public:
Virtual void display () const;
};

Void printnameanddisplay (window W)
{
STD: cout <W. Name ();
W. Display ();
}

Descriwwithscrollbars 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.

Small user-defined type

1. Its size may change. May change in the future

2. Some compilers treat "built-in types" and "user-defined types" very differently, even if they have the same underlying representation.

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.