C++11でのfinalの使用

3087 ワード

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 

/////////////////////////////////////////////////
// reference: http://en.cppreference.com/w/cpp/language/final
struct 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