C++11中final的使用

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/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

GitHubhttps://github.com/fengbingchun/Messy_Test

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值