mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-03 10:33:25 +08:00
support bazel complie this project and format code.
This commit is contained in:
33
cpp2.0/cpp11/final.cpp
Normal file
33
cpp2.0/cpp11/final.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by light on 19-11-3.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// final关键字用于两个地方
|
||||
// 第一个用在类,用于说明该类是继承体系下最后的一个类,不要其他类继承我,当继承时就会报错。
|
||||
class Base final {
|
||||
public:
|
||||
Base() {}
|
||||
|
||||
virtual void func() {}
|
||||
};
|
||||
|
||||
class Derivered : public Base { // error: cannot derive from ‘final’ base ‘Base’ in derived type ‘Derivered’
|
||||
};
|
||||
// 第二个用在虚函数,表示这个虚函数不能再被override了,再override就会报错。
|
||||
class Base1 {
|
||||
public:
|
||||
Base1() {}
|
||||
|
||||
virtual void func() final {}
|
||||
};
|
||||
|
||||
class Derivered1 : public Base1 {
|
||||
virtual void func() {} // error: overriding final function ‘virtual void Base1::func()’
|
||||
};
|
||||
int main() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user