C++11 之 override

 

override 关键字

作用:在成员函数声明或定义中, override 确保该函数为虚函数并覆写来自基类的虚函数。
位置:函数调用运算符之后,函数体或纯虚函数标识 “= 0” 之前。

使用以后有以下好处:
1.可以当注释用,方便阅读.
2.告诉阅读你代码的人,这是方法的复写.
3.编译器可以给你验证 override 对应的方法名是否是你父类中所有的,如果没有则报错.

override 使用举例

如果你想重写父类的方法,比如toString()方法:

正确的是:

public String toString() override {
	...
}

假如不小心把方法名写错了而没写 override ,这时编译器是可以编译通过的,因为编译器以为这个方法是你的子类中自己增加的方法。如:

// 注意这里的小写方法,实际上是错误的。
public String tostring() {...}

相反,如果你很机智,在知道自己要重写父类的方法,加上了 override 标签后,编译器会检查出重写方法错误,会保证你重写父类方法的正确性。

 

复制代码
#include <memory>
#include <iostream>
using namespace std; 
class Base
{
public:
            virtual void foo();
};
void Base::foo()
{
        cout << "Base::foo" <<endl;
}
 
class Derived : public  Base
{
            void foo() override; // OK: Derived::foo overrides Base::foo
};
void Derived::foo()
{
        cout << "Derived::foo" <<endl;
}
int main()
{
        Base * b = new Derived();
        b->foo();
        return 0;
} 
复制代码

 

root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
root@ubuntu:~/c++# ./over
Derived::foo

 

非虚函数

复制代码
#include <memory>
#include <iostream>
using namespace std;
class Base
{
public:
            //virtual void foo();
            void foo();
};
void Base::foo()
{
        cout << "Base::foo" <<endl;
}

class Derived : public  Base
{
            void foo() override; // OK: Derived::foo overrides Base::foo
};
void Derived::foo()
{
        cout << "Derived::foo" <<endl;
}
int main()
{
        Base * b = new Derived();
        b->foo();
        return 0;
}
复制代码

 

 

over.cpp:17:11: error: ‘void Derived::foo()’ marked ‘override’, but does not override
      void foo() override; // OK: Derived::foo overrides Base::foo
           ^

 

 

复制代码
#include <memory>
#include <iostream>
using namespace std; 
class Base
{
public:
            virtual void foo();
};
void Base::foo()
{
        cout << "Base::foo" <<endl;
}
 
class Derived : public  Base
{
            void foo(int a) override; // OK: Derived::foo overrides Base::foo
};
void Derived::foo(int a)
{
        cout << "Derived::foo" <<endl;
}
int main()
{
        Base * b = new Derived();
        b->foo(3);
        return 0;
} 
复制代码

 

复制代码
root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
over.cpp:16:11: error: ‘void Derived::foo(int)’ marked ‘override’, but does not override
      void foo(int a) override; // OK: Derived::foo overrides Base::foo
           ^
over.cpp: In function ‘int main()’:
over.cpp:25:10: error: no matching function for call to ‘Base::foo(int)’
  b->foo(3);
          ^
over.cpp:9:6: note: candidate: virtual void Base::foo()
 void Base::foo()
      ^
over.cpp:9:6: note:   candidate expects 0 arguments, 1 provided
复制代码

 

 

复制代码
#include <memory>
#include <iostream>
using namespace std;
class Base
{
public:
            virtual void foo();
};
void Base::foo()
{
        cout << "Base::foo" <<endl;
}

class Derived : public  Base
{
            void foo() ; // OK: Derived::foo overrides Base::foo
            //void foo() override; // OK: Derived::foo overrides Base::foo
};
//void Derived::foo(int a)
//{
//      cout << "Derived::foo" <<endl;
//}
int main()
{
        Base * b = new Derived();
        b->foo();
        return 0;
}
复制代码

 

root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
/tmp/cc9ksedx.o: In function `Derived::Derived()':
over.cpp:(.text._ZN7DerivedC2Ev[_ZN7DerivedC5Ev]+0x14): undefined reference to `vtable for Derived'
over.cpp:(.text._ZN7DerivedC2Ev[_ZN7DerivedC5Ev]+0x18): undefined reference to `vtable for Derived'
collect2: error: ld returned 1 exit status

 

复制代码
#include <memory>
#include <iostream>
using namespace std;
class Base
{
public:
            virtual void foo();
};
void Base::foo()
{
        cout << "Base::foo" <<endl;
}

class Derived : public  Base
{
           void Foo() override; // OK: Derived::foo overrides Base::foo
};
//void Derived::foo(int a)
//{
//      cout << "Derived::foo" <<endl;
//}
int main()
{
        Base * b = new Derived();
        //b->foo();
        return 0;
}
复制代码

 

root@ubuntu:~/c++# g++ -std=c++11  over.cpp -o over
over.cpp:16:10: error: ‘void Derived::Foo()’ marked ‘override’, but does not override
     void Foo() override; // OK: Derived::foo overrides Base::foo
          ^
root@ubuntu:~/c++#

 

posted on   tycoon3  阅读(1041)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-04-01 arm64 uefi启动

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示