摘要:C++语言的override从字面意思上,是覆盖的意思,实际上在C++中它是覆盖了一个方法并且对其重写,从而达到不同的作用。在我们C++编程过程中,最熟悉的就是对接口方法的实现,在接口中一般只是对方法进行了声明,而我们在实现时,就需要实现接口声明的所有方法。还有一个典型应用就是在继承中也可能会在子类覆盖父类的方法。希望对大家学习C++语言有所帮助。
C++语言的override从字面意思上,是覆盖的意思,实际上在C++中它是覆盖了一个方法并且对其重写,从而达到不同的作用。在我们C++编程过程中,最熟悉的就是对接口方法的实现,在接口中一般只是对方法进行了声明,而我们在实现时,就需要实现接口声明的所有方法。还有一个典型应用就是在继承中也可能会在子类覆盖父类的方法。希望对大家学习C++语言有所帮助。
公有继承包含两部分:一是“接口”(interface),二是 "实现" (implementation)。
例如Person类的几种成员函数的继承方式:
[cpp] view plain copy print? 1. class Person{ 2. public: 3. virtual void Eat() const = 0; // 1) 纯虚函数 4. virtual void Say(const std::string& msg); // 2) 普通虚函数 5. int Name() const; // 3) 非虚函数 6. }; 7. 8. class Student: public Person{ ... }; 9. class Teahcer: public Person{ ... };
1.纯虚函数
纯虚函数,继承的是基类成员函数的接口,必须在派生类中重写该函数的实现:
[cpp] view plain copy print? 1. Person *s1 = new Student; 2. s1->Eat(); // calls Student::Eat 3. 4. Person *t1 = new Ellipse; 5. t1->Eat(); // calls Teacher::Eat
若想调用基类的 Eat(),须加上 类作用域操作符 ::
[cpp] view plain copy print? 1. s1->Person::Eat(); // calls Person::Eat 2.普通虚函数
普通虚函数,对应在基类中定义一个缺省的实现 (default implementation),表示继承的是基类成员函数的接口和缺省的实现,由派生类自行选择是否重写该函数。
实际上,允许普通虚函数同时继承接口和缺省实现是危险的。 如下, CarA 和 CarB 是 Car的两种类型,且二者的运行方式完全相同。
[cpp] view plain copy print? 1. class Car{ 2. public: 3. virtual void Run(const Car& destination); 4. }; 5. class CarA: public Car{ ... }; 6. class CarB: public Car{ ... };
这是典型的面向对象设计,两个类共享一个特性 -- Run,则 Run可在基类中实现,并由两个派生类继承。
现增加一个新的飞机型号 CarC,其飞行方式与 CarA,CarB 并不相同,假如不小心忘了在 CarC 中重写新的 Fly 函数
[cpp] view plain copy print? 1. class CarC: public Car{ 2. ... // no fly function is declared 3. };
则调用 CarC 中的 Run 函数,就是调用 Car::Run,但是 CarC的运行方式和缺省的并不相同
[cpp] view plain copy print? 1. Car *pa = new CarC; 2. pa->Run(Beijing); // calls Car::Run!
这就是前面所说的,普通虚函数同时继承接口和缺省实现是危险的,最好是基类中实现缺省行为 (behavior),但只有在派生类要求时才提供该缺省行为.
方法一:
一种方法是 纯虚函数 + 缺省实现,因为是纯虚函数,所以只有接口被继承,其缺省的实现不会被继承。派生类要想使用该缺省的实现,必须显式的调用:
[cpp] view plain copy print? 1. class Car{ 2. public: 3. virtual void Run(const Run& destination) = 0; 4. }; 5. 6. void Car::Run(const Airport& destination) 7. { 8. // a pure virtual function default code for Run an Car to the given destination 9. } 10. 11. class CarA: public Car{ 12. public: 13. virtual void Run(const Car& destination) { Car::Run(destination); } 14. };
这样在派生类 CarC 中,即使一不小心忘记重写 Run函数,也不会调用 Car的缺省实现
[cpp] view plain copy print? 1. class CarC: public CAr{ 2. public: 3. virtual void Run(const Car& destination); 4. }; 5. 6. void CarC::Run(const Car& destination) 7. { 8. // code for Run a CarC Car to the given destination 9. }
方法二:
可以看到,上面问题的关键就在于,一不小心在派生类 CarC中忘记重写 Run函数,C++11 中使用关键字 override,可以避免这样的“一不小心”。
非虚函数:
非虚成员函数没有virtual关键字,表示派生类不但继承了接口,而且继承了一个强制实现(mandatory implementation),既然继承了一个强制的实现,
则在派生类中,无须重新定义继承自基类的成员函数,如下:
使用指针调用 Name 函数,则都是调用的 Person::Name()
[cpp] view plain copy print? 1. Student s1; // s1 is an object of type Student 2. 3. Person *p= &s1; // get pointer to s1 4. p->Name(); // call Name() through pointer 5. 6. Student *s= &s1; // get pointer to s1 7. s->Name(); // call Name() through pointer
如果在派生类中重新定义了继承自基类的成员函数 Name 呢?
[cpp] view plain copy print? 1. class Student : public Person{ 2. public: 3. int Name() const; // hides Person::Name 4. }; 5. 6. p->Name(); // calls Person::Name() 7. s->Name(); // calls Student::Name()
此时,派生类中重新定义的成员函数会 “隐藏” (hide) 继承自基类的成员函数
这是因为非虚函数是 “静态绑定” 的,p被声明的是 Person* 类型的指针,则通过 p调用的非虚函数都是基类中的,既使 p指向的是派生类。
与“静态绑定”相对的是虚函数的“动态绑定”,即无论 p被声明为 Person* 还是 Student* 类型,其调用的虚函数取决于 p实际指向的对象类型
重写 (override)
在程序中加override 关键字,可以避免派生类中忘记重写虚函数的错误
下面以重写虚函数时,容易犯的四个错误为例,详细阐述之
[cpp] view plain copy print? 1. class Base { 2. public: 3. virtual void fun1() const; 4. virtual void fun2(int x); 5. virtual void fun3() &; 6. void fun4() const; // is not declared virtual in Base 7. }; 8. 9. class Derived: public Base { 10. public: 11. virtual void fun1(); // declared const in Base, but not in Derived. 12. virtual void fun2(unsigned int x); // takes an int in Base, but an unsigned int in Derived 13. virtual void fun3() &&; // is lvalue-qualified in Base, but rvalue-qualified in Derived. 14. void fun4() const; 15. };
在派生类中,重写 (override) 继承自基类成员函数的实现 (implementation) 时,要满足如下条件:
一虚:基类中,成员函数声明为虚拟的 (virtual)
二容:基类和派生类中,成员函数的返回类型和异常规格 (exception specification) 必须兼容
四同:基类和派生类中,成员函数名、形参类型、常量属性 (constness) 和 引用限定符 (reference qualifier) 必须完全相同
如此多的限制条件,导致了虚函数重写如上述代码,极容易因为一个不小心而出错
C++11 中的 override 关键字,可以显式的在派生类中声明,哪些成员函数需要被重写,如果没被重写,则编译器会报错。
[cpp] view plain copy print? 1. class Derived: public Base { 2. public: 3. virtual void fun1() override; 4. virtual void fun2(unsigned int x) override; 5. virtual void fun3() && override; 6. virtual void fun4() const override; 7. }; [cpp] view plain copy print? 1. class Derived: public Base { 2. public: 3. virtual void fun1() const override; // adding "virtual" is OK, but not necessary 4. virtual void fun2(int x) override; 5. void fun3() & override; 6. void fun4() const override; 7. };
1) 公有继承
纯虚函数 => 继承的是:接口 (interface)
普通虚函数 => 继承的是:接口 + 缺省实现 (default implementation)
非虚成员函数 =>继承的是:接口 + 强制实现 (mandatory implementation)
2) 不要重新定义一个继承自基类的非虚函数 (never redefine an inherited non-virtual function
3) 在声明需要重写的函数后,加关键字 override
这样,即使不小心漏写了虚函数重写的某个苛刻条件,也可以通过编译器的报错,快速改正错误。
在使用中需要注意以下几点:
(1).覆盖的方法的标志必须要和被覆盖的方法的标志完全匹配,才能达到覆盖的效果;
(2).覆盖的方法的返回值必须和被覆盖的方法的返回一致;
(3).覆盖的方法所抛出的异常必须和被覆盖方法的所抛出的异常一致,或者是其子类;
(4).被覆盖的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行覆盖。
以上就介绍了C/C+的相关知识,希望对C/C+有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号