我可以这样做吗?
static_cast<A*>(getSomePtr());
其中 getSomePtr()
将返回 nullptr
。这样可以吗?
最佳答案
来自 Wikipedia article :
...null pointer constant:
nullptr
. It is of typenullptr_t
, which is implicitly convertible and comparable to any pointer type or pointer-to-member type. It is not implicitly convertible or comparable to integral types, except forbool
.
nullptr
可以隐式转换为任何指针类型,因此使用 static_cast
进行显式转换是绝对有效的。
关于c++ - 我可以将 nullptr 转换为其他指针类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33652629/