【发布时间】:2013-03-10 22:47:43
【问题描述】:
我知道我可以获得指向类或结构的数据成员的指针,但以下代码的最后一行无法编译:
struct abc
{
int a;
int b;
char c;
};
int main()
{
char abc::*ptt1 = &abc::c;
void *another_ptr = (void*)ptt1;
}
为什么我不能将 ptt1 转换为 another_ptr?我们说的是指针,所以一个指针应该与另一个指针具有相似的维度(尽管在概念上有所不同)?
【问题讨论】:
-
错误信息是什么?
-
我正在使用 MSVC2012:错误 C2440:'type cast':无法从 'char abc::*' 转换为 'void *'
-
不要在 c++ 中使用 c-casts。坚持使用 c++ 强制转换(在本例中为 reinterpret_cast)。
-
@RedX:但是在您知道它是哪种转换之前,您不能选择 C++ 转换。在这种情况下,您无法知道它是哪种转换,因为没有任何类型的转换可用:-)
标签: c++ casting type-conversion pointer-to-member