C++11 new Features-the use of pure virtual functions and final specifiers

Source: Internet
Author: User
In my opinion, the reason why C + + is suitable for architectural design is that it can be programmed for the future. For example, you can use Public/protected/private to restrict the access of a derived class to a base class member, you can define an abstract class that requires an interface that a derived class must implement, and so on.

When we declare/define a virtual function, the derived class can choose to override this virtual function or choose not to overwrite it. In this regard, C + + also provides the necessary methods to require derived classes to override some methods, which can also be seen as a programming for the future.

Pure virtual function

This is not a new technology, and the function is very simple: once a base class declares a pure virtual function, its derived classes must override this function when instantiated.

The Sub::f1 () method is commented out in this example, so a compilation error occurs on line 27th.

Final specifier

Generally speaking, there should be a means of prohibiting coverage if there is a requirement to cover the method. But I don't know why, this feature was introduced into the c++11. The use of the method is simple: Just add the final specifier after the function declaration.

The code basically does not change, except that the BASE::F1 () declaration is replaced with the final specifier. If the Sub::f1 () method is defined, a compile error is generated.

Author's opinion

Based on the author's personal understanding, this article puts the pure virtual function and the final specifier together for illustration. If there are inaccuracies, please add and correct them.

Related articles:

Details c++11 final and override specifiers

Auto and Decltype differences and linkages in c++11 new features

Related videos:

C Language Tutorials

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.