【发布时间】:2014-09-09 13:52:22
【问题描述】:
我有一个双变量i,它被转换成一个名为pointer的空指针:
double i = 3;
void *pointer = &i;
当我想将 void 指针转换回双精度时,我使用了:
double p = *((double*) pointer);
我想将 void 指针转换为我将作为 char* 发送的类型:
char* myType= typeid(i).name();//get the name of type of "i"
int p = *((myType*) pointer); // how to implement?
有可能吗?
【问题讨论】:
-
您不应该在 C++ 中使用 C 样式转换。首选
dynamic_cast和static_cast。 -
@RedX,你能提供样品吗?谢谢
-
你可以到处投。编译器将编译。大量未定义的行为,但编译器将关闭并编译比 (myType*) 更迟,这毫无意义。
-
一个变量不包含它所包含的类型的信息(在运行时)。你不能仅仅尝试使用
typeid并期望它能够工作(而且它是一个 GNU 插件并且会产生额外的开销)
标签: c++ casting void-pointers