【发布时间】:2012-07-09 01:20:04
【问题描述】:
在 C++ 中,使用 const void * 作为函数的参数类型而不是 void * 有什么价值吗?由于void * 是不透明的,除了用户执行reinterpret_cast 之外,是否存在任何修改风险,在这种情况下,他们同样可以在const void * 上执行const_cast,因此人们真的会买任何东西吗?我之所以问,是因为我使用了一个用于共享指针的实用程序模板类,它提供了对 void 的专门化以避免 void & 问题,但没有为 const void 提供专门化,因此我想知道这是否只是一个疏忽,还是应该永远需要吗?
【问题讨论】:
-
不需要
reinterpret_cast,static_cast足以转换为任何其他类型。
标签: c++ constants void-pointers reinterpret-cast const-cast