C++多态意义探究

之前有看过一个例子:

 1 #include <iostream> 
 2 using namespace std; 
 3 
 4 class Father
 5 {
 6 public:
 7     void Face()
 8     {
 9         cout << "Father's face" << endl;
10     }
11 
12     virtual void Say()
13     {
14         cout << "Father say hello" << endl;
15     }
16 };
17 
18 class Son:public Father
19 {
20 public:     
21     void Say()
22     {
23         cout << "Son say hello" << endl;
24     }
25 };
26 
27 void main()
28 {
29     Son son;
30     Father *pFather=&son; 
31     pFather->Say();
32 }

总感觉这边可以就在编译的时候确定绑定是哪一个函数,没有必要等到运行时在确定,然后就在思考多态的意义。

后来发现如果代码变成这样时:

猜你喜欢

转载自www.cnblogs.com/longlively/p/9289276.html
今日推荐