Files
CPlusPlusThings/c++2.0/override.cpp
T
Light-City 4f8b242a10 update
2019-11-05 17:27:32 +08:00

24 lines
781 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// Created by light on 19-11-3.
//
#include <iostream>
using namespace std;
class Base {
public:
Base(){}
virtual void func() {}
};
class Derivered:public Base{
virtual void func(int) override {} //error: virtual void Derivered::func(int) marked override, but does not override
};
// override用于虚函数,上面的virtual void func(int)实际上不是重写父类的虚函数,而是定义一个新的虚函数,
// 我们的本意是重写虚函数,当不加overrride的时候,这样写编译器不会报错,
// 那如果像下面加上override的话,则会报错,表示告诉了编译器,我确实要重写,但写错了,没有重写,于是就报错了,
// 这样就能给我们对虚函数的重写做检查!
int main() {
}