简体   繁体   中英

Function-style casting a pointer

Chapter 4.11.3 of the book C++ Primer says the following:

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

I get that C-style casting a pointer works like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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