1. 程式人生 > >Overloaded Fuction 調用——到底使用的是誰的函數

Overloaded Fuction 調用——到底使用的是誰的函數

spa printf ret 使用 ron tdi base ase strong

#include <stdio.h>

class Base{
public:
    int a(){
        return 1;
    }
};
class Inherit:public Base{
public:
    int a(){
        return 2;
    }
};
int main(int argc, char **argv) {
    Base *p=new Inherit();
    printf("%d\n",p->a());
}

輸出是1。說明調用的是父類的函數。

class Base{
public:
    virtual
int a(){ return 1; } };

輸出是2。說明調用的是子類的函數。

所以當父類的函數是虛函數,調用的會是子類函數。

Overloaded Fuction 調用——到底使用的是誰的函數