C++11 之 override

摘要:
Override关键字:在成员函数声明或定义中,Override确保函数是虚拟函数,并从基类重写虚拟函数。位置:在函数调用运算符之后,在函数体或纯虚拟函数ID“=0”之前。

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++#

免责声明:文章转载自《C++11 之 override》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇shell脚本之nginx的安装Java并发容器--ConcurrentLinkedQueue下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

那些年java MD5加密字符编码的坑

相信做过MD5加密的童鞋都遇到过字符编码的坑,一般加密出来的结果和其他人不一样都是字符编码不一致导致的,比如类文件的字符编码、浏览器的字符编码等和对方不一致,所以就需要转码统一字符。 以下是笔者转码过程中遇到的坑: 不要new String("XXXX".getBytes("UTF-8")),之后将转码后的字串传入MD5去加密,会遇到意想不到的效果,有的字...

C#操作access和SQL server数据库代码实例

    在C#的学习中,操作数据库是比较常用的技术,而access和sql server 数据库的操作却有着不同。那么,有哪些不同呢?              首先,需要引用不同的类。因为有着不同的数据引擎。     access:using System.Data.OleDb;     sql server:using System.Data.SqlC...

java 接收 char字符型

import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Scanner;public class GetChar {public static void main(String[] args)throws Exception {//通过扫描类输入Scan...

java的http请求实例

package vqmp.data.pull.vqmpull.common.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpEntity; import org.springframework.http...

Windows系统中CreateFileMapping实现的共享内存及用法

在32位的Windows系统中,每一个进程都有权访问他自己的4GB(232=4294967296)平面地址空间,没有段,没有选择符,没有near和far指针,没有near和far函数调用,也没有内存模式。 每个进程都有独立的4GB逻辑地址空间,32位的Windows系统允许每一个进程独立访问自己的内存,即独立于其它进程,也即它自己的32位逻辑地址空间。操作...

C# 6.0新特性

C#6.0新特性  一、C#发展历程 一、C#6.0新特性 转自:https://www.cnblogs.com/yinrq/p/5600530.html 1、字符串插值 (String Interpolation) 字符串拼接优化 Before: var Name = "joye.net"; var Results = "Hello" + Name;/...