"Effective C + +" study notes-clause 20

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************






Iv. Designs and Declarations



Rule 20:prefer Pass-by-reference-to-const to Pass-by-value

rule 20: Ning to Pass-by-reference-to-const Replace Pass-by-value





1. The amount .....

By default, C + + passes an object to a function by value, unless you specify otherwise, the function parameter is the initial value of the copy of the actual argument, and the caller obtains a duplicate of the function return value. These duplicate systems are produced by the object's copy constructor, which makes Pass-by-value a time-consuming operation.

Let's take a look at the following example:

Class Person  {public: Person    ();    For simplification, omit the parameter    virtual ~person ();    .. private:    std::string name;    std::string address;}; Class Student:public person  {public:    Student ();    For simplification, omit parameter    ~student ();    ... private:    std::string schoolname;    Std::string schooladdress;};

now that the declaration is complete, do the following:

BOOL Validatestudent (Student s);    The function accepts the parameter student Plato;bool Platoisok = Validatestudent (Plato) in the By value method;    Calling functions

At this point, there is no doubt that the copy constructor of student will be called to initialize S with Plato as a blueprint. Similarly, when Validatestudent returns s will be destroyed. Therefore, for this function, the delivery cost of the parameter is: one student copy constructor + one student destructor.

However, the person class and the Student class each have two strings, when Student calls a constructor, the person two string generation, Student two string constructs, this is 4 constructors, the destructor is the same, so the total increase Consumption: 6 constructors + 6 destructors .



2.pass-by-reference-to-const

In this way, avoid the unnecessary consumption of--pass-by-reference-to-const.

> Advantages ①: High efficiency

Ps:const function-ensures that incoming parameters are not modified.

> Benefits ②: Avoid slicing (object cutting) issues

(When a derived class object is passed as a by-value, it is treated as a base class object, calling the constructor of the base class object, so that the characteristics of the derived class object will be removed)



3. When do I use it?

looking at the bottom of the C + + compiler, you will find that references is often implemented as pointers, so pass by reference usually means that pointers are actually passed. So, if the object is a built-in type , it will be more efficient by using pass by value.

STL iterators and Function Objects Are also designed as pass by value.

That's--the change in the rules depends on which part of the c++--you use.



4. A corollary

? Corollary: The built-in types are fairly small, so all small types are pass-by-value, even if the user-defined class.

Obviously, the inference is wrong.

> Small object does not mean that its copy constructor is not time consuming. Many objects--including most STL containers--contain more than one pointer, but copying such objects replicates everything that the pointers refer to, which can be time consuming.

> Small objects have no time-consuming copy constructors, or there may be an efficiency controversy. Some compilers treat "built-in type" and "User-defined type" differently, even if they have the same underlying representation. For example, some compilers refuse to put an object consisting of only a double into a buffer, but are happy to do so on a regular basis on a bare double.

> as a user-defined type, Pass-by-value is not necessarily available because its size is easily changed. Even when you switch to another C + + compiler, it is possible to change the size of the type.

? In general, you can reasonably assume that the only object that "Pass-by-value is not time consuming" is the built-in types and STL iterators and function objects. As for the rest, please follow the advice of these terms and replace pass-by-value with Pass-by-reference-to-const.


5. Please remember

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

☆ The above rules do not apply to built-in types and STL iterators and function objects. Pass-by-value is more appropriate for them.




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************

"Effective C + +" study notes-clause 20

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.