#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