(21条消息) 023:Fun和Do_Simon_Paul的博客-CSDN博客
1 #include <iostream> 2 using namespace std; 3 class A { 4 private: 5 int nVal; 6 public: 7 void Fun() 8 { cout << "A::Fun" << endl; }; 9 void Do() 10 { cout << "A::Do" << endl; } 11 }; 12 class B:public A { 13 public: 14 virtual void Do() 15 { cout << "B::Do" << endl;} 16 }; 17 class C:public B { 18 public: 19 void Do( ) 20 { cout <<"C::Do"<<endl; } 21 void Fun() 22 { cout << "C::Fun" << endl; } 23 }; 24 void Call( 25 // 在此处补充你的代码 26 B& p 27 ) { 28 p.Fun(); p.Do(); 29 } 30 int main() { 31 C c; 32 Call( c); 33 return 0; 34 }
如果B不是派生而来且没有fun函数,那么会报错。除非B里有fun函数且为虚函数,p才会调用C里面的fun函数
1 #include <iostream> 2 using namespace std; 3 /*class A { 4 private: 5 int nVal; 6 public: 7 void Fun() 8 { cout << "A::Fun" << endl; }; 9 void Do() 10 { cout << "A::Do" << endl; } 11 }; */ 12 class B/*:public A*/{ 13 public: 14 virtual void Do() 15 { cout << "B::Do" << endl;} 16 }; 17 class C:public B { 18 public: 19 void Do( ) 20 { cout <<"C::Do"<<endl; } 21 void Fun() 22 { cout << "C::Fun" << endl; } 23 }; 24 void Call( 25 // 在此处补充你的代码 26 B& p 27 ) { 28 p.Fun(); p.Do(); 29 } 30 int main() { 31 C c; 32 Call( c); 33 return 0; 34 }
报错截图:
标签:Do,函数,fun,报错,023,Fun From: https://www.cnblogs.com/balabalabubalabala/p/16639362.html