大佬教程收集整理的这篇文章主要介绍了C++ override和final关键字(详解版),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
// This program has a subtle error in the virtual functions. #include <iostream> #include <memory> using namespace std; class Base { public: virtual void functionA(int arg) const{Cout << "This is Base::functionA" << endl; } }; class Derived : public Base { public: virtual void functionA(long arg) const{ cout << "This is Derived::functionA" << endl; } }; int main() { // Base pointer b points to a Derived class object. shared_ptr<Base>b = make_shared<Derived>(); // Call virtual functionA through Base pointer. b->functionA(99); return 0; }程序输出结果:
This is Base::functionA
在该程序中,Base 类指针 b 指向 Derived 类对象。因为 functionA 是一个虚函数,所以一般可以认为 b 对 functionA 的调用将选择 Derived 类的版本。//This program demonstrates the use of the override keyword. #include <iostream> #include <memory> using namespace std; class Base { public: virtual void functionA(int arg) const { cout << "This is Base::functionA" << endl;} }; class Derived : public Base { public: virtual void functionA(int arg) const override{ cout << "This is Derived::functionA" << endl; } }; int main() { // Base pointer b points to a Derived class object. shared_ptr<Base>b = make_shared<Derived>(); // Call virtual functionA through Base pointer. b->functionA(99); return 0; }程序输出结果:
This is Derived::functionA
以上是大佬教程为你收集整理的C++ override和final关键字(详解版)全部内容,希望文章能够帮你解决C++ override和final关键字(详解版)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。