0
double d = 43.56;
int m = d;
cout<<(char *)&m<<endl; //works fine, it prints: + = 43
cout<<(char *)m<<endl; //this doesn't work, char can't be made into a  pointer
cout<<reinterpret_cast<char *>(&m)<<endl; //works fine, prints: +
cout<<static_cast<char *>(&m)<<endl; //Does not work

My question is, why the last line of code doesn't work?

Compiler error message: invalid static_cast from int* to type char*

Shouldn't static_cast be able to convert this?

Thanks in advance.

KeyC0de
  • 4,728
  • 8
  • 44
  • 68

1 Answers1

1

Here is the complete list of what can be done with static_cast. And there is no option to cast pointer to one class to pointer to another non related class. Simply because this is not a standard C++ behavior.
However one can still use the c-style cast, reinterpret_cast or even "casting" with union.

Teivaz
  • 5,462
  • 4
  • 37
  • 75