If the cast in C style (float)i;
or float(i);
, then in essence in both cases the constructor is called and a temporary object is created? But what about pointers, if you cast int* p
to (float*)p
, then why can't you write float*(p)
and what happens with (float*)p
?
UPD:
@VladD ; For static_cast
, something is not clear. For example, there is a C style:
char* str = "123456789"; int* pi = (int*)str;
and with static_cast
will be:
char* str = "123456789"; void* pv = static_cast<void*>(str); int* pi = static_cast<int*>(pv);
so why should I bring in, and not immediately to int
?
Is this an intermediate cast made because in C there are no restrictions in terms of assigning a pointer of one type to a pointer of another type, but in C ++ is there? That is, it turns out that if you write in C ++ (int*)str
, then it will be decomposed into static_cast<void*>
and static_cast<int*>
? And how does reinterpret_cast
then ignore this restriction in C ++? Or does it also use static_cast
?
char* str = "123456789"; int* pi = reinterpret_cast<int*>(str);
@avp , I understand that this will not work in C:
int a; float b = float(a);
because in C there are no classes and the constructor will not be called, but in the constructor it is implicitly reduced. But how then does this work in C:
int a; float b = (float)a;
because if in C ++ with (float) a; static_cast
or const_cast
or reinterpret_cast
is selected, what happens in C when (float)a
?
const_cast
*static_cast
*reinterpret_cast
*dynamic_cast
* C-style cast - VladDint
tofloat
one * machine instruction cvtsi2ss is used . In C ++ it will be the same. - avp