C + + reading notes static conversion (static_cast)
Relative to dynamic type conversions, static type conversions do a wider range of conversions, but only if they must be of a related type , that is, the compiler must think it is understandable , for example, a non polymorphic class hierarchy, and the pointer of a grandchild object is easy. For example, the postgraduate object pointer to the student object pointer conversion, or vice versa. Since void * to any type of pointer can be converted to a fusion, the pointer conversion of void to student object can also be done by static_cast, or from the space requested by the local heap space to the shaping array space. Even sometimes, to turn void * to a pointer to a Polymorphic object, you have to go through the static_cast too.
VOID fn (void * pd, void * PA)
{
Student s ("John");
Graduatestudent GS ("Smith");
Student * PS = static_cast<student *> (&GS);
Graduatestudent * pgs = static_cast<graduatestudent *> (PS);
Student * pp = static_cast<student *> (PD);
Account * px = static_cast<account *> (PA);
saving * PSa = dynamic_cast<saving *> (px);
int * p = static_cast<int *> (malloc (100));
}
If the student class has nothing to do with the Graduatestudent class, then the PS and pgs pointers can only be null (#define NULL 0), and if PD is not a pointer, and because PP is a pointer, the value of the PP can only be null; If the PA is not a pointer, The value of PX can only be null, and the value that is compromised to the PSA is null; this is the check Help for type-safe conversions with static_cast, which is better than the "anti-theft" type (expression) of typed conversion form without base. Because of the non null Judgment of the pointer, static_cast can avoid the operation after the conversion.
Static_cast conversions are not specific to pointers, and can be manipulated as long as they are related to the type of conversion. It's mostly about the type of determination, not the polymorphism. The type conversion of polymorphism is done by dynamic_cast.