运维经–目录
marked override, but does not override
c++11中引入了override关键字,被override修饰的函数其派生类必须重载。
#include <iostream>
class Object
{
public:
Object(){};
virtual ~Object(){};
virtual void fun() override
{
std::cout << "Object::fun" << std::endl;
}
};
class Base:public Object
{
public:
Base(){};
virtual ~Base(){};
void fun();
};
void Base::fun()
{
std::cout << "Base::fun" << std::endl;
}
int main(){
Object* b = new Base;
b->fun();
return 0;
}
编译:
[frank@LAPTOP-0OCJTGJR class]$ g++ -g -std=c++11 test_class.cpp
test_class.cpp:9:18: error: ‘virtual void Object::fun()’ marked override, but does not override
virtual void fun() override
^