"Effective C + +"Clause 20: Prefer to replace pass-by-value with Pass-by-reference-to-constBy default, C + + passes an object to a function by value. Unless you know otherwise, the function parameter is the initial value of the copy of the actual
Key points of this article:1, try to replace Pass-by-value with Pass-by-reference-to-const. The former is more efficient and can avoid cutting problems.2. This rule does not apply to both the built-in type and the iterator and function object types
Effective C + + ' ning to pass-by-reference-to-const replace Pass-by-value 'By default, C + + passes an object to a function by value (a way that inherits from C). Unless you specify otherwise, the function parameter is the initial value of the
Effective C + + ' ning to pass-by-reference-to-const replace Pass-by-value 'By default, C + + passes an object to a function by value (a way that inherits from C). Unless you specify otherwise, the function parameter is the initial value of the
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
By default, C ++ uses the by Value Method to pass objects to functions. The real parameters of a function are all based on the response of the actual parameter as the initial value, and the call end obtains a replica of the function return value.
Preface:We've all used the value-passing method of C, so how does the value transfer work in C + + cases?Like what:int foo (int x); int I;foo (i);1. Get a copy of I within the program2. Passing a copy to the Foo function3.foo backThese replicas are
clause 20: Prefer to replace pass-by-value with Pass-by-reference-to-const
By default, C + + passes objects to (or from) functions in by value, a way that inherits from C. Unless you specify otherwise, the function arguments are based on the actual
Reading Notes effctive c ++ Item 20 is preferentially transmitted by const-reference (by-reference-to-const) instead of by value (by value), constreference1. Passing parameters by value will be efficient
By default, C ++ transfers objects to or from
(1)
If the pass-by-value parameter is passed when a function is called, the function parameters take the actual copy of the real parameter as the initial value, and the call end obtains a replica of the function return value.
See the following code:
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 (slicing problem).
The above rules do not apply to built-in types, as well as STL
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.
Replace Pass-by-value with Pass-by-reference-to-const as much as possible. The former is usually more efficient because it avoids the call to copy constructors and destructors, and avoids cutting problems.is a good example of slicing problems:
The instantaneous argument (argument) to the formal parameter (parameter) of a function call takes place in a value or address pass.----"Plain C + +" Bao YuThe above values are passed by value, and the address is passed by address and by reference,
*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************Iv. Designs and Declarations
Rule 20:prefer Pass-by-reference-to-const to
Clause 19: design class is like design type
Treat class design as type design
In C ++, just like other object-oriented programming languages, a new type can be defined by defining a new class. As a C ++ developer, you spend a lot of time expanding
Take Uitextfield to illustrate:1.Ns_class_available_ios(2_0) @interface uitextfield:uicontrol , nscoding > @endThe contents of the middle of these two lines of content,such as @property (nonatomic,copy) NSString *text; is the Uitextfield
(iv). Design and declaration_______________________________________________________________________________________________________________ _____________________Article 18: Make the interface easy to use, but not easily misused#1Importing new types
6.1 Clause 18: Make interfaces easy to use correctly and hard to use incorrectly)
Clause 13 indicates that the customer places the newly applied resources in the smart pointer to avoid resource leakage. A constructor provided by STD: tr1: shared_ptr
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.