C++什么是命名的强制转换

201*_*ker 2 c++ casting

以下旧式演员阵容的命名演员阵容是什么?

const string *ps;
void *pv;

pv = (void*)ps; // <--- this
Run Code Online (Sandbox Code Playgroud)

pv = static_cast<void*>(const_cast<string*>(ps));吗?

小智 7

pv = const_cast<string *>(ps);
Run Code Online (Sandbox Code Playgroud)

足够好 - void *可以任何(非限定的)数据(对象)指针类型隐式赋值.

(当然,出于同样的原因,在const void * 没有任何演员的情况下直接分配也会有效.)