Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which kind of cast is from Type* to void*?

In C++ for any data type I can do the following:

Type* typedPointer = obtain();
void* voidPointer = typedPointer;

which cast is performed when I assign Type* to void*? Is this the same as

Type* typedPointer = obtain();
void* voidPointer = reinterpret_cast<void*>( typedPointer );

or is it some other cast?

like image 804
sharptooth Avatar asked Feb 04 '10 08:02

sharptooth


2 Answers

It is a standard pointer conversion. Since it is a standard conversion, it doesn't require any explicit cast.

If you want to reproduce the behavior of that conversion with an explicit cast, it would be static_cast, not reinterpret_cast.

Be definition of static_cast given in 5.2.9/2, static_cast can perform all conversions that can be performed implicitly.

like image 145
AnT Avatar answered Sep 28 '22 06:09

AnT


It is not a cast, it is implicit conversion. Casts are explicit by definition. It is no more a cast than:

char c = 'a';
int i = c;

is.

like image 42
Alok Singhal Avatar answered Sep 28 '22 05:09

Alok Singhal