Effective C + + clause 37: Never redefine inherited default parameter values

Source: Internet
Author: User

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 bound to the function in the actual object being referred to, and a derived type pointer P2 points to a derived object, when the function is called by P2. The function in the derived is called directly, and its parameter value is also the parameter value corresponding to the function in the derived class.

#include <iostream>using namespace Std;class a{public:enum Color {red,green,blue}; A () {cout<< "Construct a" <<ENDL;} virtual void Dodraw () {Draw ();} private:virtual void Draw (Color color=blue) const{cout<< "Base draw:" <<color<<endl;}}; Class B:public a{public:b () {cout<< "Construct B" <<ENDL;} virtual void Dodraw () {Draw ();} private:virtual void Draw (Color color=red) const{cout<< "derived draw:" <<color<<endl;}}; int main () {b b; A *p=&b; B *p2=&b;p->dodraw ();p 2->dodraw (); return 0;}


In the above program, all of the two virtual functions in a are rewritten in class B, so that the Dodraw function is called by P and P2, which is actually the Dodraw in the call B, and the Dodraw function in B invokes the draw function in B. The actual argument to draw is the default argument in B: Red

#include <iostream>using namespace Std;class a{public:enum Color {red,green,blue}; A () {cout<< "Construct a" <<ENDL;} virtual void Dodraw () {Draw ();} private:virtual void Draw (Color color=blue) const{cout<< "Base draw:" <<color<<endl;}}; Class B:public a{public:b () {cout<< "Construct B" <<ENDL;} private:virtual void Draw (Color color=red) const{cout<< "derived draw:" <<color<<endl;}}; int main () {b b; A *p=&b; B *p2=&b;p->dodraw ();p 2->dodraw (); return 0;}


The above program, B and a in the Dodraw function are derived from a, called by P and P2 when Dodraw, is called the Class AThe dodraw function,when Dodraw calls the draw function, dynamically binds to the draw function in the object B actually referred to, which is determined at run time, but the default argument of the draw function is statically bound, which is the blue value in Class A, which is already determined by the compiler. Therefore, if you redefine the inherited default parameter values, there is a strange phenomenon: the virtual function is used in the derived class, and the parameter is used in the base class.



Effective C + + clause 37: Never redefine inherited default parameter values

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.