Is it possible to cast a void pointer to the type of its content? | SoloLearn: Learn to code for FREE!

0

Is it possible to cast a void pointer to the type of its content?

I mean something like this, bool b; void* ptr = &b; cout<< *( type(b)* )ptr;

5/24/2020 8:06:55 PM

Mustafa K.

1 Answer

New Answer

+2

Well yea bool b = true; void* p = &b; std::cout << *(decltype(b)*)p; But this only applies if b is in scope and you know where p is pointing to. Most of the time you'll want to avoid void* unless you know what you're doing.