Effective C + + Item 37 never again defines inherited default values

Source: Internet
Author: User

This article is Senlie original, reprint please retain this address:Http://blog.csdn.net/zhengsenlie


Experience: Never again the default number of parameters. Because the default values are static bindings, and the virtual function--the only thing you should overwrite--is dynamically bound

Demo Sample:


Analytical:
Because the PR is a Shape *, the default number of references is shape::red. Because the PR points to Rectangle, the call is Rectangle::d Raw
Although this is a C + + content, only can be included in the "Python Source code anatomy", Python's default parameters are placed in a static object pycodeobject at compile time

Correction: An alternative design using virtual functions. such as NVI: Make a public non-virtual function inside the base class call the private virtual function

Class Shape{public:enum Shapecolor {red, Green, blue};void draw (shapecolor color = red) const{dodraw (color);} private:virtual void Dodraw (shapecolor color) const = 0;//};class rectangle:public shape{public:...private:virtual void Draw (shapecolor color) const; assigning different default values}

Effective C + + Item 37 never again defines inherited default values

Related Article

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.