Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which conversion is right in C++?

I want to covert a void* to char* reinterpret_cast and static_cast, which one is fit for ?static_cast<char*> or reinterpret_cast<char*>

like image 545
user1400047 Avatar asked Sep 04 '12 09:09

user1400047


Video Answer


1 Answers

It's largely a question of style. static_cast can do any conversion which is the opposite of an implicit conversion (and which doesn't remove const or volatile). Since char* to void* is implicit, static_cast would seem indicated; the usual rule is to use static_cast in preference to reinterpret_cast whenever possible.

Given that this use is particularly dangerous, some coding guidelines might prefer reinterpret_cast, to signal this fact.

like image 104
James Kanze Avatar answered Oct 10 '22 02:10

James Kanze