OnEventData(void* pData)
{......
// pData is a void* pData,
// EventData is a structure e.g.// typedef struct _EventData {// std::string id;// std:: string remote_id;// } EventData;
// On Some Situation a void pointer *pData// has been static_casted as// EventData* pointer
EventData *evtdata = static_cast<EventData*>(pData);.....}
int a=rand(); // Random number.
int* pa1=reinterpret_cast<int*>(a); // OK. Here developer clearly expressed he wanted to do this potentially dangerous operation.
int* pa2=static_cast<int*>(a); // Compiler error.int* pa3=dynamic_cast<int*>(a); // Compiler error.
int* pa4=(int*) a; // OK. C-style cast can do such cast. The question is if it was intentional or developer just did some typo.
*pa4=5; // Program crashes.
B1:+0: pointer to virtual method table of B1+4: value of int_in_b1
B2是形式:
B2:+0: pointer to virtual method table of B2+4: value of int_in_b2
那么D的内存数据结构必须类似于:
D:+0: pointer to virtual method table of D (for B1)+4: value of int_in_b1+8: pointer to virtual method table of D (for B2)+12: value of int_in_b2+16: value of int_in_d
int i = 13;void *p = &i;auto *pi = static_cast<int*>(p);
对于函数,我们必须使用reinterpret_cast两次:
#include<iostream>
using any_fcn_ptr_t = void(*)();
void print(int i){std::cout << i <<std::endl;}
int main(){//Create type-erased pointer to function:auto any_ptr = reinterpret_cast<any_fcn_ptr_t>(&print);
//Retrieve the original pointer:auto ptr = reinterpret_cast< void(*)(int) >(any_ptr);
ptr(7);}