L.S. Roth
L.S. Roth

Reputation: 447

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);

Upvotes: 1

Views: 230

Answers (1)

Jeffrey
Jeffrey

Reputation: 11400

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;

Upvotes: 5

Related Questions