当前位置:网站首页>(P13)final关键字的使用
(P13)final关键字的使用
2022-06-12 08:01:00 【喜欢打篮球的普通人】
1.final修饰函数
- 把final认为是一刀切的作用
如果使用 final 修饰函数,只能修饰虚函数,这样就能阻止子类重写父类的这个函数了:
class Base
{
public:
virtual void test()
{
cout << "Base class...";
}
};
class Child : public Base
{
public:
void test() final
{
cout << "Child class...";
}
};
class GrandChild : public Child
{
public:
// 语法错误, 不允许重写
void test()
{
cout << "GrandChild class...";
}
};
- 在上面的代码中一共有三个类:
基类:Base
子类:Child
孙子类:GrandChild
- test() 是基类中的一个虚函数,在子类中重写了这个方法,但是不希望孙子类中继续重写这个方法了,因此在子类中将 test() 方法标记为 final,孙子类中对这个方法就只有使用的份了。
2.final修饰类
使用 final 关键字修饰过的类是不允许被继承的,也就是说这个类不能有派生类。
- Child 类是被 final 修饰过的,因此 Child 类不允许有派生类 GrandChild 类的继承是非法的,Child 是个断子绝孙的类。
class Base
{
public:
virtual void test()
{
cout << "Base class...";
}
};
class Child final: public Base
{
public:
void test()
{
cout << "Child class...";
}
};
// error, 语法错误
class GrandChild : public Child
{
public:
};
边栏推荐
- Record the treading pit of grain Mall (I)
- Detailed explanation of Google open source sfmlearner paper combining in-depth learning slam -unsupervised learning of depth and ego motion from video
- Compiling principle on computer -- function drawing language (III): parser
- Ceres optimizer usage (self use)
- visual studio2019的asp.net项目添加日志功能
- (P17-P18)通过using定义基础类型和函数指针别名,使用using和typedef给模板定义别名
- Vision Transformer | CVPR 2022 - Vision Transformer with Deformable Attention
- Compiling principle on computer -- functional drawing language (V): compiler and interpreter
- Final review of Discrete Mathematics (predicate logic, set, relation, function, graph, Euler graph and Hamiltonian graph)
- Cmake can't find the solution of sophus
猜你喜欢

Vins technical route and code explanation

Some summaries of mathematical modeling competition in 2022

DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ...阅读笔记

MinGW offline installation package (free, fool)

PPP agreement

Architecture and performance analysis of convolutional neural network

Improvement of hash function based on life game

Dynamic simulation method of security class using Matlab based Matpower toolbox

MATLAB image processing - cosine noise removal in image (with code)

Windows10 configuration database
随机推荐
Understanding and analysis of state estimation and Kalman filter
Three data exchange modes: line exchange, message exchange and message packet exchange
Compiling principle on computer -- functional drawing language (V): compiler and interpreter
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water
OpenMP task 原理與實例
Summary of 3D point cloud construction plane method
TMUX common commands
Process terminated
Rich dad, poor dad Abstract
The Poisson regression model (posion) is constructed by GLM function of R language, and the poisgof function of epidisplay package is used to test the goodness of fit of the fitted Poisson regression
(P25-P26)基于非范围的for循环、基于范围的for循环需要注意的3个细节
Numpy generates row vectors and column vectors (1024 water medal text)
Compiling principle on computer -- function drawing language (II): lexical analyzer
S-msckf/msckf-vio technical route and code details online blog summary
Leetcode notes: biweekly contest 69
Cmake can't find the solution of sophus
Leetcode notes: biweekly contest 71
System service configuration service - detailed version
Dynamic simulation method of security class using Matlab based Matpower toolbox
N-order nonzero matrix AB, matrix ab=0, then the rank of a and B is less than n