C++11中final的使用

来源:互联网 发布:阿里云带宽调整 编辑:程序博客网 时间:2024/06/14 04:10

final: Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from.

When used in a virtual function declaration or definition, final ensures that the function is virtual and specifies that it may not be overridden by derived classes. The program is ill-formed (a compile-time error is generated) otherwise.

When used in a class definition, final specifies that this class may not appear in the base-specifier-list of another class definition (in other words, cannot be derived from). The program is ill-formed (a compile-time error is generated)otherwise. final can also be used with a union definition, in which case it has no effect (other than on the outcome of std::is_final), since unions cannot be derived from)

final is an identifier with a special meaning when used in a member function declaration or class head. In other contexts it is not reserved and may be used to name objects and functions.

C++11 also adds the ability to prevent inheriting from classes or simply preventing overriding methods in derived classes. This is done with the special identifier final.

final blocks further derivation of a class and further overriding of a virtual function.

The C++11 keyword final has two purposes. It prevents inheriting from classes, and it disables the overriding of a virtual function.

Sometimes you don’t want to allow derived class to override the base class’ virtual function.C++ 11 allows built-in facility to prevent overriding of virtual function using final specifier.

The final keyword in C++11 can be applied to either an entire class or a method. When applied to a class, it signifies that the class is closed to derivation; that is, you cannot create a class derived from a final class. The second way is when applying final to a method, which prevents the method from being overridden by a derived class (though it is still possible to create subclasses).

C++11的关键字final有两个用途:(1)、禁止虚函数被重写;(2)、禁止基类被继承。

在派生类中,可以同时使用overried和final。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "final.hpp"#include <iostream>/////////////////////////////////////////////////// reference: http://en.cppreference.com/w/cpp/language/finalstruct Base {virtual void foo();};struct A : Base {virtual void foo() final; // A::foo is final// void bar() final; // Error: non-virtual function cannot be final};struct B final : A { // struct B is final// void foo(); // Error: foo cannot be overridden as it's final in A};// struct C : B { }; // Error: B is final////////////////////////////////////////////////////////// reference: http://blog.smartbear.com/c-plus-plus/use-c11-inheritance-control-keywords-to-prevent-inconsistencies-in-class-hierarchies/struct A_ {virtual void func() const;};struct B_ : A_ {void func() const override final; //OK};// struct C_ : B_ { void func()const; }; //error, B::func is final

GitHub:https://github.com/fengbingchun/Messy_Test

0 0
原创粉丝点击