#include <iostream>
using namespace std;
class dissection {
int x;
public:
void test() {
cout<<"Test base";
}
void caller(){ test(); }
};
class dissectionDerived: public dissection {
int x;
public:
void test(){
cout<< "Test derived";
}
};
int main(int argc, char ** argv) {
dissectionDerived derived;
derived.caller();
return 0;
}
在上面的代碼示例中,輸出是「測試基地」。我想到的方式是,由於派生函數沒有函數調用者它可以調用基類函數,但由於對象的實際類型是dissectionDerived它將能夠調用測試函數解剖派生類。這是因爲重載解析在基類範圍內找到最接近的測試函數後就停止了嗎?當通過使用派生對象的另一個基本函數調用基本和派生調用基時的常用函數?
如果調用者函數可以從派生函數調用,爲什麼它不能成爲派生類中重載解析的一部分?
我已經使用-cg克++編譯器標誌,然後做了objectfile的OBJ轉儲,輸出如下所示:
SYMBOL TABLE:
0000000000000000 l d .text._ZN10dissection4testEv 0000000000000000 .text._ZN10dissection4testEv
0000000000000000 l d .text._ZN10dissection6callerEv 0000000000000000 .text._ZN10dissection6callerEv
0000000000000000 w F .text._ZN10dissection4testEv 000000000000001d _ZN10dissection4testEv
0000000000000000 *UND* 0000000000000000 __gxx_personality_v0
0000000000000000 *UND* 0000000000000000 _ZSt4cout
0000000000000000 *UND* 0000000000000000 _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
0000000000000000 w F .text._ZN10dissection6callerEv 000000000000001a _ZN10dissection6callerEv
C++ FILT _ZN10dissection4testEv給出解剖::測試()
謝謝!
如果你爲此解釋了爲什麼是 – JustSid
,那麼我將upvote它是'test()'必須是虛擬的,而不是'caller()'。 – juanchopanza
謝謝,juanchopanza。我一定失去了理智。 – Oswald