我不确定这个问题是否已经得到解决。我正在检查Stack overflow函数中的一个,并产生了这个疑问。
让我们先检查代码:
#include <string>
#include <map>
#include <iostream.h>
class MyClass
{
public:
virtual int Func()
{
return 0;
}
int Func2()
{
return 0;
}
};
class MyClass2 : public MyClass
{
public:
int Func( )
{
return 1;
}
int Func2()
{
return 1;
}
};
class Processor
{
private:
typedef int (MyClass::*MemFuncGetter)();
static std::map<std::string, MemFuncGetter> descrToFuncMap;
public:
static void Initialize();
void Process(MyClass* m, const std::string&);
};
std::map<std::string, Processor::MemFuncGetter> Processor::descrToFuncMap;
void Processor::Initialize()
{
descrToFuncMap["Func"]=&MyClass::Func;
descrToFuncMap["Func2"]=&MyClass::Func2;
};
void Processor::Process(MyClass* ms, const std::string& key)
{
std::map<std::string, MemFuncGetter>::iterator found = descrToFuncMap.find(key);
if(found != descrToFuncMap.end())
{
MemFuncGetter memFunc = found->second;
int dResult = (ms->*memFunc)();
cout << "Result is : "<< dResult <<endl;
}
}
int main(int argc, char* argv[])
{
Processor::Initialize();
Processor p;
MyClass *pMC2 = new MyClass2;
p.Process(pMC2, "Func");
p.Process(pMC2, "Func2");
delete pMC2;
pMC2 = NULL;
return 0;
}
在这个例子中,结果如预期的那样:
Result is : 1
Result is : 0
但是当我使用VC 6 调试工具调试,并观察 Processor::Process 中的 memFunc 值时,发现以下数值:
在pMC2上调用p.Process("Func");
memFunc 0x004011bd [thunk]:`vcall {0,{flat}}
在p.Process(pMC2, "Func2")中调用
memFunc 0x0040118b MyClass::Func2(void)
I didn t understand thunk and flat in "[thunk]:`vcall {0,{flat}}" ? Can anyone help me to understand the internals here ?