Item 37: Never redefine the inherited default parameter value of a function (the default parameter value obtained through inheritance)
By Scott Meyers
Translator: fatalerror99 (itepub's nirvana)
Release: http://blog.csdn.net/fatalerror99/
We started
Key points:
Do not redefine an inherited default parameter, because the default parameters are static binding, and virtualFunction-the only thing you should override-is dynamic binding..
Virtual functions are dynamically bound, but the
It is always wrong to redefine an inherited non-virtual function. The scope of this article is limited to "virtual function with default parameters ".
Virtual functions are dynamically bound, but the default parameter is static binding.
The
Since the inherited non-virtual function is incorrectly redefined (see the previous clause), this clause limits the problem: never redefine a virtual function that inherits the default parameter value.
(1)
Virtual functions are dynamically bound,
Do not redefine the inherited default parameter values
Let's simplify the problem from the very beginning. Default parameters can only exist as part of a function. In addition, only two types of functions
The function can give the default parameter value in advance when declaring it, and if the argument is given, the actual parameter value is used, otherwise the default value is given beforehand.int Add (int5,int6return x + y;} int Main () { Add
If the default argument is not passed when a function is called, the default parameter within the function is a reference to the function's default parameter property, __defaults__.Such asdef func (arg1=[]): arg1.append (2)If there are no
--------------------------------------------------------------------------------
Clause 24: exercise caution when selecting between function overload and default parameter values
The reason for obfuscation between function overloading and Setting
In python, as the default parameter must be immutable object, if it is a mutable object, there will be problems, a little attention, will be transferred into traps, especially beginners, such as I (┬_┬). Let's look at an example.1 defAdd
Label: Use the problem code C ++ sp c program objectVirtual functions are dynamically bound, while default parameter values are statically bound. When the base class and derived class set the default parameter values for the same virtual function,
Transferred from Liaoche's official website:
The default parameters are useful, but improper use can also fall into the pit. The default parameter has a maximum pit: The value of the default parameter may change unconsciously in the process of
Step on a token: use the default parameter with caution, and use the parameter with caution.
I always thought. net compiler will compile the default parameters into the reload mode for the main DLL call, until recently want to extend a method does
Introduction to PHP default parameter functions
In PHP, the default invocation of a function is supported, which is to specify a default value for the parameter. The previous section describes the reference parameter function of the PHP function,
This article describes the default parameters of the ECMAScript6 function. For more information, see the new features in the language update, which are extracted from the needs of millions of developers, standard adoption can reduce the programmer's
Remember before a colleague spit the slot in Java there is no default parameter this thing, want to achieve the purpose of the default parameters can only rely on crappy function overloading. But today we found a pit of default parameters in C +
This article describes the default parameters of the ECMAScript6 function. For more information, see the new features in the language update, which are extracted from the needs of millions of developers, standard adoption can reduce the programmer's
The virtual function is dynamically bound, while the default parameter value of the virtual function is statically bound. Using a base-type pointer p to point to a derived class object, when the virtual function is called by P, it is dynamically
1. Functions for default parameter valuesThe C + + language allows you to specify a default value for a function when it is defined or some form parameter (parameter) by writing "= Default" after the corresponding parameter, and if the value of the
Why is the Python function definition default parameter must point to the immutable object?The following is a personal understanding, if there is a wrong place, welcome to discussCause analysis: When defined, the function allocates the memory space
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.