Difference between revisions of "Intermediate C++ Game Programming Tutorial 19"

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 12: Line 12:
 
* Using dynamic cast to figure out to what derived type a pointer to a base type is actually pointing [https://youtu.be/g-NGBFCn3co?t=54s 0:54]
 
* Using dynamic cast to figure out to what derived type a pointer to a base type is actually pointing [https://youtu.be/g-NGBFCn3co?t=54s 0:54]
 
** A dynamic cast can be used on pointers and on references
 
** A dynamic cast can be used on pointers and on references
** To check on a reference, use <code>if( DerivedClass* p_temp = dynamic_cast<DerivedClass*>(&ref_to_BaseClassObject) )</code>
 
 
** To check on a pointer, use <code>if( DerivedClass* p_temp = dynamic_cast<DerivedClass*>(p_to_BaseClassObject) )</code><br />(note 1: if the cast fails, it will return a nullptr of the DerivedClass type)<br />(note 2: nullptr is #defined to 0 and thus evaluates to false inside an if statement)
 
** To check on a pointer, use <code>if( DerivedClass* p_temp = dynamic_cast<DerivedClass*>(p_to_BaseClassObject) )</code><br />(note 1: if the cast fails, it will return a nullptr of the DerivedClass type)<br />(note 2: nullptr is #defined to 0 and thus evaluates to false inside an if statement)
 +
** To check on a reference, one could use <code>DerivedClass& val_temp = dynamic_cast<DerivedClass&>(BaseClassObject)</code>, but that will throw an exception when the cast fails so it cannot be used inside an if statement
 
* WORK-IN-PROGRESS
 
* WORK-IN-PROGRESS
  

Revision as of 04:53, 15 October 2019

In this video Chili teaches us how to figure out what our polymorphic pointers are actually pointing to (aka "type discovery"). Just note that although we can do this, it is generally a weaksauce way to go about things. Virtual functions are 1000% more hype than type discovery bullshit. Oh yeah, we also finally see all the C++ style casts united.

Topics Covered

  • dynamic_cast<T*> and dynamic_cast<T&>
  • const_cast
  • Overview of all C++ style casts
  • RTTI with typeid()
  • The type_info class

Video Timestamp Index

Tutorial 19

  • Using dynamic cast to figure out to what derived type a pointer to a base type is actually pointing 0:54
    • A dynamic cast can be used on pointers and on references
    • To check on a pointer, use if( DerivedClass* p_temp = dynamic_cast<DerivedClass*>(p_to_BaseClassObject) )
      (note 1: if the cast fails, it will return a nullptr of the DerivedClass type)
      (note 2: nullptr is #defined to 0 and thus evaluates to false inside an if statement)
    • To check on a reference, one could use DerivedClass& val_temp = dynamic_cast<DerivedClass&>(BaseClassObject), but that will throw an exception when the cast fails so it cannot be used inside an if statement
  • WORK-IN-PROGRESS

Source Code

Note that the code for this video is in a different branch called "casting". You will not find it in the master branch.

See also