Default parameters are bound during compilation rather than dynamic binding.

Source: Internet
Author: User

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.

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.