Advanced Casts Static and Dynamic Creative Commons License

  • Slides: 9
Download presentation
Advanced Casts Static and Dynamic Creative Commons License – Curt Hill.

Advanced Casts Static and Dynamic Creative Commons License – Curt Hill.

Static casting • Recall the problems with casting – Automatic casting diminishes readability –

Static casting • Recall the problems with casting – Automatic casting diminishes readability – The C cast looks funny and tends to confuse, because of precedence – The C++ cast looks like a function or constructor but cannot help with multi keyword types • There is a revision of the C++ cast that corrects the problems of the C++ cast • It is called the static cast Creative Commons License – Curt Hill.

Static Cast • Form: static_cast < type name > (value) • static_cast is a

Static Cast • Form: static_cast < type name > (value) • static_cast is a reserved word • The <> and () are needed to delimit values • Example: static_cast <unsigned char> (line[i]) • Easier to read but adds little functionality Creative Commons License – Curt Hill.

Polymorphism • Suppose a base class and derived classes – Recall the person, student,

Polymorphism • Suppose a base class and derived classes – Recall the person, student, employee, grad hierarchy • I have a pointer which is of the type of the base class – say student • This pointer may refer to a base class instance or a derived class instance – Either student or grad • The base class has one or more virtual functions • What happens when I invoke any of the virtual functions using this pointer? – It determines which type of class I have – at run time – and then invokes the correct function Creative Commons License – Curt

Issues • This mechanism works automatically – But not without issues • Even if

Issues • This mechanism works automatically – But not without issues • Even if you know that this points at a grad you cannot use anything unique to grad • The dynamic cast allows us to upcast or downcast manually – The cast is dependent on what is referred to by the pointer Creative Commons License – Curt

Dynamic casting • Form: dynamic_cast <T> (ptr) • T must be a pointer type

Dynamic casting • Form: dynamic_cast <T> (ptr) • T must be a pointer type to a reference to a defined class type • ptr must be a reference or pointer • Typically both T and ptr are in the same inheritance hierarchy Creative Commons License – Curt

Example: • Consider: student * sp; person * pp; … sp=dynamic_cast <student *> (pp);

Example: • Consider: student * sp; person * pp; … sp=dynamic_cast <student *> (pp); – If the conversion is not safe then a NULL is produced – If pp points to a student or grad (the proper pointer or its descendents) then the value of the pointer is copied over – If it points to an employee or person then we get a NULL Creative Commons License – Curt

Commentary • The dynamic cast is always safe • If it is the desired

Commentary • The dynamic cast is always safe • If it is the desired pointer type or a descendent we get a non-null pointer – If not a null pointer • This is extremely handy with polymorphic pointers • Sometimes we are brave and want a conversion, safe or not – Then a reinterpret cast is in order Creative Commons License – Curt

Reinterpret cast • This is a mechanism to convert a pointer to an integer

Reinterpret cast • This is a mechanism to convert a pointer to an integer and the reverse • It is not safe • Form: j = reinterpret_cast <int> (sp); • I do not yet see the point, other than an easier to search form of casting Creative Commons License – Curt