We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
课上,老师给出了派生类函数重写覆盖基类函数的定义:
下面给出重写覆盖的一般实现:
#include <iostream> class Base { public: virtual int f() { return 0; } }; class Derive: public Base { public: int f() { return 1; } }; int main() { Derive d; std::cout << ((Base&)d).f();//1 return 0; }
而在一般情况下,返回值不同时,编译失败:
class Base { public: virtual int f() { return 0; } }; class Derive: public Base { public: void f()//编译失败 { return; } };
在作业的选择题中,有如下问题,部分同学有所疑问:
#include <iostream> using namespace std; class Base { int* x; public: Base(){x = new int[10];} Base(const Base& other){fn();} // (1) virtual void fn(){} virtual void g1(){} virtual Base& g2(){} ~Base(){delete [] x;} }; class Derive: public Base { int* y; public: Derive(){y = new int[10];} Derive(const Derive& other):Base(other){fn();} void fn(){} void g1() const {} Derive& g2(){} ~Derive(){delete [] y;} }; void fn(){ Base* b = new Derive(); delete b; // (*) } int main(){ fn(); return 0; }
C. Derive::g2()的返回值为Derive&,Base::g2()的返回值为Base&。Derive::g2()仍能重写覆盖Base::g2(),不会出现编译错误。
许多同学不太明白这种重写覆盖的实现方式,以及为何返回值不同。下面做简要说明。
在题目中的这种特殊情况下,函数返回值可以不同,具体而言, 可以是基类函数返回基类引用,派生类函数返回派生类引用:
class Base { public: virtual Base& f() {return *this;} }; class Derive: public Base { public: Derive& f() {return *this;} };
也可以是基类函数返回基类指针,派生类函数返回派生类指针:
class Base { public: virtual Base* f() {return this;} }; class Derive: public Base { public: Derive* f() {return this;} };
当然,返回的并不一定是当前类,例如:
class Baser {} b; class Deriver: public Baser {} d; class Base { public: virtual Baser& f() {return b;} }; class Derive: public Base { public: Deriver& f() {return d;} };
也是可以的。 那么派生类成员函数返回的类型会被向上转换吗?答案是肯定的。这里给出例子:
#include <iostream> class Base { public: virtual Base& f() {return *this;} virtual void test2() {return;} }; class Derive: public Base { public: void test() {std::cout << "is Derive&\n";} Derive& f() {return *this;} void test2() { f().test(); } }; int main() { Derive d; d.test(); d.f().test(); d.test2(); Base& b = d; b.test();//tes错误,t()不是Base的成员,无法调用 b.f().test();//错误,b.f()的类型是Base&,同样无法调用test() b.test2();//调用成功,输出"is Derive&\n",说明Derive::f()的返回值确实是Derive&,但在外部调用时被转换为Base& return 0; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
函数重写覆盖的返回值相关
重写覆盖的定义
课上,老师给出了派生类函数重写覆盖基类函数的定义:
重写覆盖的一般情况
下面给出重写覆盖的一般实现:
而在一般情况下,返回值不同时,编译失败:
重写覆盖中返回值不同的情况
在作业的选择题中,有如下问题,部分同学有所疑问:
许多同学不太明白这种重写覆盖的实现方式,以及为何返回值不同。下面做简要说明。
在题目中的这种特殊情况下,函数返回值可以不同,具体而言,
可以是基类函数返回基类引用,派生类函数返回派生类引用:
也可以是基类函数返回基类指针,派生类函数返回派生类指针:
当然,返回的并不一定是当前类,例如:
也是可以的。
那么派生类成员函数返回的类型会被向上转换吗?答案是肯定的。这里给出例子:
The text was updated successfully, but these errors were encountered: