I am not sure if the question is already addressed. I was checking the one of the Stack overflow function and got this doubt.
Lets check the code first:
#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
But when I debugged using VC 6 debugger and observed the value of memFunc in Processor::Process and found following values:
In p.Process(pMC2, "Func"); call
memFunc 0x004011bd [thunk]:`vcall'{0,{flat}}'
In p.Process(pMC2, "Func2"); call
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 ?