I have been trying to wrap my head around the differences between type casting and type conversion. What i've learnt is that essentially when the underlying value or bit representation of one type is changed to another value, we have performed a conversion. While in pure type casting we simply tell the compiler to treat the bit-pattern as another type. I used 'pure' because a casting may lead to a conversion in which case it is called an 'explicit conversion'.
I have thought of two examples to illustrate this difference. First:
float x = 1.5;
float* p1 = &x;
int* p2 = (int*) p1;
int i = *p2;
Here casting a pointer type performs no operation on the pointer value. So it's pure casting. In this case it has lead to undefined behavour as we get the int value based on f1.5's bit representation, 1069547520 to be precise.
Second:
B* b = new D();
D* d = static_cast<D*>(b);
Where D is derived from B. Here an implicit conversion is done in the first line. In the second line too, the cast is actually a conversion.These are conversions because the pointer value may be changed, adjusting the value if necessary to point to the complete D object or the B sub-object (I havent completely understood how offsets work though.)
Am i correct in calling pointer casts of user-defined classes conversions? If so then static_cast above has also performed a conversion(explicitly) whereas this answer i read calls casts a different concept altogether:
https://stackoverflow.com/a/34268988/1219638 -
The standard conversions are implicit conversions with built-in meanings and are separate concepts to things like static_cast or C-style casts.
Also why is a conversion of user-defined class pointer called a standard conversion?
Can i ask one last question? I have learnt that a C-style cast will act like a static_cast when the types are related. Does that mean that a C-style cast will also calculate offsets if it has to?