c++ - 函数样式转换指针

标签 c++

《 C++ Primer》一书的第4.11.3章说:

In early versions of C++, an explicit cast took one of the following two forms:

type (expr); // function-style cast notation
(type) expr; // C-language-style cast notation
我得到C样式强制转换指针的工作方式如下:
int*  ip = nullptr;
void* vp = (void*) ip;
但是,我找不到如何使用函数样式的强制转换。下面的代码不起作用,我明白了为什么。我该如何工作?
int*  ip = nullptr;
void* vp = void*(ip);

最佳答案

您可以使用这种方式:

using voidPointer = void*;

int*  ip = nullptr;
void* vp = voidPointer(ip);
之所以有效,是因为它使类型成为单个单词。另外,这也可以:
typedef void* voidPointer;

关于c++ - 函数样式转换指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62881513/

相关文章:

c++ - 为什么我可以在私有(private)类型上使用 auto?

c++ - 如何使用多态性优雅地转换 switch+enum

c++ - 我无法使用 fstream 读取 .dat 文件

c++ - OpenCV - 从图像分割树

c++ - Operator[][] 重载 - 改变值

c++ - 这个程序有什么问题?

c++ - 使用预处理器宏组合另一个宏调用

c++ - 检测到您的 conan 配置文件设置和 CMake 之间的编译器版本不匹配

c++ - 函数模板和常规重载

c++ - 线程和结构通信