Say I have an object:
void *tmpValue;
and say I know that tmpValue points to a double.
A way to cast this into a double is to do the following:
double* dblPtr = (double*) tmpValue;
double dbl = *dblPtr;
But why does a direct casting from void* to double not work?
double dbl = (double) tmpValue; //error: "cannot convert from 'void*' to 'double'
Thanks in advance.
Interpreting a pointer (a memory address) as a floating-point value is not a sensible operation, and it probably fails on your platform because
void *anddoubleare not even the same size.What you want to do is interpret the pointer as
double *and dereference thatdouble *pointer, as in your second code snippet.You can do this in one line:
But, hey, this is C++. Better to do