When I try to use a static_cast to cast a double* to an int*, I get the following error:
invalid static_cast from type ‘double*’ to type ‘int*’
Here is the code:
#include <iostream>
int main()
{
double* p = new double(2);
int* r;
r=static_cast<int*>(p);
std::cout << *r << std::endl;
}
I understand that there would be problems converting between a double and an int, but why is there a problem converting between a double* and an int*?
Aside from being pointers,
double*andint*have nothing in common. You could say the same thing forFoo*andBar*pointer types to any dissimilar structures.static_castmeans that a pointer of the source type can be used as a pointer of the destination type, which requires a subtype relationship.