`vcall'{0,{flat}}' 在下面的例子中是什么意思?
我不确定问题是否已经解决.我正在检查堆栈溢出功能之一,并得到了这个疑问.
I am not sure if the question is already addressed. I was checking the one of the Stack overflow function and got this doubt.
让我们先检查代码:
#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;
}
在这个例子中,结果如预期:
In this example, the result is as expected:
Result is : 1
Result is : 0
但是当我使用 VC 6 调试器进行调试并观察 Processor::Process 中 memFunc 的值时,发现以下值:
But when I debugged using VC 6 debugger and observed the value of memFunc in Processor::Process and found following values:
在 p.Process(pMC2, "Func");打电话
In p.Process(pMC2, "Func"); call
memFunc 0x004011bd [thunk]:`vcall'{0,{flat}}'
在 p.Process(pMC2, "Func2");打电话
In p.Process(pMC2, "Func2"); call
memFunc 0x0040118b MyClass::Func2(void)
我不明白 "[thunk]:`vcall'{0,{flat}}" 中的 thunk 和 flat ?任何人都可以帮助我了解这里的内部结构吗?
I didn't understand thunk and flat in "[thunk]:`vcall'{0,{flat}}" ? Can anyone help me to understand the internals here ?
维基百科上对 thunk 的含义有非常详尽的解释
There is a pretty thorough explanation of what thunk means over on WikiPedia
http://en.wikipedia.org/wiki/Thunk
thunk 的要点是在运行时访问 C++ 虚拟表的机制.它被设置为为对象的运行时类型调用适当的虚函数.
The gist of is thunk is the mechanism by which the C++ virtual table is accessed at runtime. It is setup to call the appropriate virtual function for the runtime type of the object.
至于 vcall{0,{flat}} 的含义,我不是 100% 确定.我猜测是它报告了thunk访问方法的值.
As to what the vcall{0,{flat}} means I am not 100% sure. My guess is that it's reporting the values by which the thunk is accessing the method.
- 0:vtable 中的偏移量
- {flat}:继承层次是扁平的,不是多重的