鍍金池/ 問答/C  C++  網(wǎng)絡(luò)安全/ 下面這個(gè)C++代碼為什么輸出y y1而不是y x1呢?為什么和直接調(diào)z->

下面這個(gè)C++代碼為什么輸出y y1而不是y x1呢?為什么和直接調(diào)z->xx()的結(jié)果不同?

#include <iostream>
using namespace std;
class a {
public:
    void virtual x() {
        cout << "x" << endl;
    }
    void xx() {
        cout << "x1" << endl;
    }
};

class b :public a {
public:
    void x() {
        cout << "y" << endl;
        xx();
    }
    void xx() {
        cout << "y1" << endl;
    }
};

int main()
{
    b b1;
    a* z = &b1;
    z->x();
}
回答
編輯回答
抱緊我

和"動(dòng)態(tài)邊編"無關(guān). 這裏的問題只是name hiding導(dǎo)致的罷了.

因爲(wèi)這是在標(biāo)準(zhǔn)文檔中有著明確規(guī)定的.

§ 6.3.10

  1. The declaration of a member in a derived class (Clause 13) hides the declaration of a member of a base class of the same name;

需要注意的是這裏只要求的name, 不要求signature, 那麼讓我們來看這份代碼,

using namespace std;
class a {
public:
    void virtual x() {
        cout << "x" << endl;
    }
    void xx() {
        cout << "x1" << endl;
    }
    void xx(int)
    {
        cout << "call: void xx(int))" << endl;
    }
};

class b :public a {
public:
    void x() {
        cout << "y" << endl;
        xx(1);
    }
    void xx() {
        cout << "y1" << endl;
    }
};

int main()
{
    b b1;
    a* z = &b1;
    z->x();
}
prog.cc:21:9: error: too many arguments to function call, expected 0, have 1; did you mean 'a::xx'?
        xx(1);
        ^~
        a::xx
prog.cc:11:10: note: 'a::xx' declared here
    void xx(int)
         ^
1 error generated.
1

clang告訴我們?cè)?code>b中是看不到a中的void xx(int). 爲(wèi)什麼呢? 就是因爲(wèi)上面所說的, 同時(shí)滿足了兩個(gè)條件:

  1. xx是name.
  2. xx同時(shí)在ab中出現(xiàn)

所以結(jié)果就是hides the declaration.

好, 我們來坐下實(shí)驗(yàn), 因爲(wèi)這兩個(gè)條件要同時(shí)滿足, 即成交集, 如果我們破壞了第二個(gè)條件, 把b中的xx函數(shù)移除, 那麼這個(gè)hiding會(huì)怎麼樣呢? 理論上就會(huì)消失, 到底是不是呢?

#include <iostream>
using namespace std;
class a {
public:
    void virtual x() {
        cout << "x" << endl;
    }
    void xx() {
        cout << "x1" << endl;
    }
    void xx(int)
    {
        cout << "call: void xx(int))" << endl;
    }
};

class b :public a {
public:
    void x() {
        cout << "y" << endl;
        xx(1);
    }
};

int main()
{
    b b1;
    a* z = &b1;
    z->x();
}
y
call: void xx(int))

果然通過編譯並且正確輸出了.

2018年4月2日 19:39
編輯回答
傲嬌范

這是屬于動(dòng)態(tài)聯(lián)編的問題,可以百度學(xué)習(xí)一下

實(shí)現(xiàn)動(dòng)態(tài)聯(lián)編的條件:
(1)公有繼承
(2)調(diào)用虛函數(shù)
(3)通過基類的對(duì)象指針或者對(duì)象的引用調(diào)用虛函數(shù)

在你的代碼中z是一個(gè)基類指針,指向了一個(gè)子類對(duì)象,并且z調(diào)用的函數(shù)x()是一個(gè)虛函數(shù),所以發(fā)生了動(dòng)態(tài)聯(lián)編,調(diào)用的是b1的x()函數(shù),在這個(gè)函數(shù)中又調(diào)用了xx()函數(shù),此函數(shù)與基類中的xx()函數(shù)重名,所以基類的xx()函數(shù)被隱藏了,所以不論xx()函數(shù)是否為虛函數(shù),調(diào)用的始終是b1自己的。

使用z去調(diào)用xx()函數(shù),由于xx()函數(shù)不是虛函數(shù),不滿足動(dòng)態(tài)聯(lián)編的條件,所以不發(fā)生動(dòng)態(tài)聯(lián)編,因而調(diào)用的是基類中的xx()函數(shù)

2017年11月2日 16:12