C++11 Override and Final keywords

Source: Internet
Author: User

Before c++11, there was no inheritance control keyword. Disabling further derivation of a class is possible but tricky. To prevent users from overloading a virtual function in a derived class, you have to think backwards.

C + + 11 added two inheritance control keywords: override and final. Override ensures that overloaded functions declared in a derived class have the same signature as the virtual function of the base class. Final blocks further derivation of classes and further overloading of virtual functions.

virtual function overloading

A derived class can overload a member function declared in a base class, which is the basis for object-oriented design. However, a simple operation such as overloading a function can also be an error. The two common errors about overloaded virtual functions are as follows:
inadvertently overloaded
Signatures do not match

First, let's analyze the syndrome of unintentional overloading. You may have inadvertently overloaded this virtual function by declaring a member function that has the same name and signature as a virtual member function of the base class. Compilers and readers who read the code are hard to find because they often assume that this new function is meant to be overloaded with base class functions:

class a{public:virtual void func ();}; Span class= "Hljs-class" >class b: A{} ; class f{}; class d: a, f{public: void func (); //meant to declare a new function but //accidentally overrides A::func};   

Reading the above code, you cannot determine whether the member function D::func () intentionally overloads the A::func (). It can also be an accidental overload because the argument list and the name of the two functions happen the same.

Signature mismatches are a more common scenario. This causes an unexpected creation of a new virtual function (instead of overloading an existing virtual function), as shown in the following example:

class  G{public: virtual void func(int);};class H: G{public: virtual void func(double); };

In this case, the programmer intended to overload G::func () in class H. However, because H::func () has a different signature, the result creates a new virtual function instead of overloading the base-class function:

*p=new H;p->func(5); //calls G::fp->func(5.0); // calls H::f

In this case, not all compilers will give a warning, and sometimes that will be set to suppress the warning.

Based on the two errors above

In C++11, these two bugs can be eliminated by using the New keyword override. override explicitly indicates that a function is an overload of a virtual function in a base class . More importantly, it examines the signature mismatch between the base class virtual function and the overloaded function in the derived class. If the signature does not match, the compiler issues an error message.

Let's take a look at how override eliminates the signature mismatch bug:

class G{public: virtual void func(int);};class H: G{public: virtual void func(double) override; //compilation error};

When processed to the H::func () declaration, the compiler looks for a virtual function that matches it in a base class.

Final functions and classes

The C++11 keyword final has two uses. First, it prevents inheritance from the class, and second, prevents overloading of a virtual function. Let's look at the final class first.

Programmers often insist on deriving from std::vector without being aware of the risks. In C++11, no subclass type is declared as follows:

class TaskManager {/*..*/} final; class PrioritizedTaskManager: public TaskManager {}; //compilation error: base class TaskManager is final

Similarly, you can suppress a virtual function from being further overloaded by declaring it final. If a derived class attempts to overload a final function, the compiler will error:

class a{pulic:virtual void func () const;}; class b: A {pulic: void func () const  Override final; //ok}; class c: B {pulic: void func () CONST; //error, B::func is final};          

Whether C::func () is declared as override is okay, and once a virtual function is declared final, the derived class can no longer reload it.

Transferred from: http://www.cnblogs.com/minggong/p/6457726.html

C++11 Override and Final keywords

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.