我需要在我的其他代码中验证虚拟成员函数的代码。那么如何获得指向正确代码的指针呢?
class MyInterface {
public:
virtual void VirtualMethod() = 0;
};
class MyImplementation : public MyInterface {
private:
int m_value;
public:
MyImplementation() : m_value(0) { }
virtual void VirtualMethod() {
m_value = 1;
}
};
void main(int argc, char* argv[])
{
MyInterface* pInterface = new MyImplementation();
// In my real code on the following line, we do not have access to the declaration of MyImplementation
unsigned int* pFunctionPointer = (unsigned int*)pInterface->VirtualMethod;
// Now we want to access the compiled code of MyImplementation::VirtualMethod.
printf("0x%08x\n", *pFunctionPointer);
}
在我的实际代码中,我根本无法从“main”函数访问 MyImplementation 声明,如果你明白我的意思的话。