C + +: Use compound (composition) instead of private inheritance

Source: Internet
Author: User

Compound methods (composition) and private inheritance can all represent the form of a new class implemented from a class, by using protected members, and by overriding the virtual function.

Choose the compound method preferentially. Reason:

1. The compound method can be implemented to override the virtual function and then prevent the derived class from redefining the virtual function;

2. You can minimize the compilation dependency of a widget by containing only the declarations (comps) of the class and avoiding the header file (inheritance) that contains the base class;

Exception: Private inheritance, which makes Ebo (the most optimized for whitespace base classes), is important in the case of some whitespace base classes, but rarely occurs. So the first compound.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.