1.概要
c++11 新特性 override 检查函数不可以被重写
2.代码
#include <iostream>
using namespace std;
namespace T1 {
class A
{
public:
virtual void fun() {
cout << "a fun \n";
}
};
class A1 :A
{
public:
void fun() override {
cout << "a1 fun \n";
}
};
void test() {
cout << "test1-----------------------\n";
A* a = (A*)new A1();
a->fun();
}
}
namespace T2 {
class A
{
public:
void fun() {
cout << "a fun \n";
}
};
class A1 :A
{
public:
/* override: It is not a virtual function that cannot be overwritten
void fun() override {
cout << "a1 fun \n";
}
*/
};
void test() {
cout << "test1----------override: It is not a virtual function that cannot be overwritten-------------\n";
A* a = (A*)new A1();
a->fun();
}
}
int main()
{
T1::test();
T2::test();
std::cout << "Hello World!\n";
}
2.运行结果
test1-----------------------
a1 fun
test1----------override: It is not a virtual function that cannot be overwritten-------------
a fun
Hello World!