#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();
}
和"動態邊編"無關. 這裏的問題只是name hiding導致的罷了.
因爲這是在標準文檔中有着明確規定的.
§ 6.3.10
需要注意的是這裏只要求的
name
, 不要求signature
, 那麼讓我們來看這份代碼,clang告訴我們在
b
中是看不到a
中的void xx(int)
. 爲什麼呢? 就是因爲上面所說的, 同時滿足了兩個條件:name
.a
和b
中出現所以結果就是
hides the declaration
.好, 我們來坐下實驗, 因爲這兩個條件要同時滿足, 即成交集, 如果我們破壞了第二個條件, 把
b
中的xx
函數移除, 那麼這個hiding
會怎麼樣呢? 理論上就會消失, 到底是不是呢?果然通過編譯並且正確輸出了.