View a program
# Include <iostream> using namespace STD; Class A {public: Virtual void fun (INT number = 10) {STD: cout <":: fun with number "<number <Endl ;}; Class B: Public A {public: Virtual void fun (INT number = 20) {STD :: cout <"B: Fun with number" <number <Endl ;}}; int main () {B; A & A = B;. fun (); Return 0;} // dynamic binding of virtual functions => B, non-A, the default real parameter is determined at compilation => 10, not 20
Output: B: Fun with number 10
Clause 38: Do not redefine the inherited default parameter value
The reasons for these terms become very obvious: virtual functions are dynamically bound, and default parameter values are statically bound. This means that you may eventually call a virtual function that is defined in the derived class but uses the default parameter value in the base class.
Why does C ++ stick to this unconventional approach? The answer is related to the running efficiency. If the default parameter value is dynamically bound, the compiler must find a proper default value for the virtual function at runtime, this is more complex than the mechanism used to determine the default value in the compilation phase. This kind of choice is intended to improve the speed and simplify the implementation, So now everyone can feel the efficiency of running the program. Of course, if you ignore the suggestions in these terms, it will bring chaos.
Reprinted: http://blog.csdn.net/coolingcoding/article/details/8045978
Default parameters are bound during compilation rather than dynamic binding.