简体   繁体   English

函数式转换指针

[英]Function-style casting a pointer

Chapter 4.11.3 of the book C++ Primer says the following: C++ Primer 一书的第 4.11.3 章说:

In early versions of C++, an explicit cast took one of the following two forms:在 C++ 的早期版本中,显式转换采用以下两个 forms 之一:

type (expr); // function-style cast notation
(type) expr; // C-language-style cast notation

I get that C-style casting a pointer works like this:我得到 C 风格的指针转换是这样的:

int*  ip = nullptr;
void* vp = (void*) ip;

However, I can't find how to do this with a function-style cast.但是,我找不到如何使用函数样式转换来做到这一点。 The code below does not work, and I can see why.下面的代码不起作用,我明白为什么。 How can I get this to work?我怎样才能让它工作?

int*  ip = nullptr;
void* vp = void*(ip);

You can use this way:你可以这样使用:

using voidPointer = void*;

int*  ip = nullptr;
void* vp = voidPointer(ip);

This works because it makes the type a single word.这是有效的,因为它使类型成为一个单词。 Alternatively, this works too:或者,这也有效:

typedef void* voidPointer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM