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

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 18: Line 18:
 
* Using a static cast [https://youtu.be/g-NGBFCn3co?t=6m20s 6:20]
 
* Using a static cast [https://youtu.be/g-NGBFCn3co?t=6m20s 6:20]
 
** Suppose: you have a polymorphic <code>DerivedClass</code> that has a nonvirtual member function <code>Foo()</code>
 
** Suppose: you have a polymorphic <code>DerivedClass</code> that has a nonvirtual member function <code>Foo()</code>
** You have a pointer <code>BaseClass* ptr = new DerivedClass</code> to the polymorphic base class that you know points to the derived class
+
** You have a pointer <code>BaseClass* ptr = new DerivedClass</code> to the polymorphic base class that you know points to the derived class (the "dynamic type")
 
** To call the derived class' member function, you can use a static cast to DerivedClass pointer <br /><code>static_cast<DerivedClass*>(ptr)->Foo();</code>
 
** To call the derived class' member function, you can use a static cast to DerivedClass pointer <br /><code>static_cast<DerivedClass*>(ptr)->Foo();</code>
 
** ... or a static cast to DerivedClass referencee <br /><code>static_cast<DerivedClass&>(*ptr).Foo();</code>
 
** ... or a static cast to DerivedClass referencee <br /><code>static_cast<DerivedClass&>(*ptr).Foo();</code>

Revision as of 03:56, 16 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<new-type>(expression) to determine to what derived type a pointer to a polymorphic base type is actually pointing 0:54
    • A dynamic cast can be used on pointers and on references
    • To cast (from and to) a pointer, use
      if( DerivedClass* ptr_temp = dynamic_cast<DerivedClass*>(ptr_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 cast (from and to) a reference, you can 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)
    • A dynamic cast can be a costly operation (depends on the inheritance tree, nature of the cast, compiler)
    • Dynamic cast only works for types that have at least one virtual function (the operation needs information from the vtable)
  • Using a static cast 6:20
    • Suppose: you have a polymorphic DerivedClass that has a nonvirtual member function Foo()
    • You have a pointer BaseClass* ptr = new DerivedClass to the polymorphic base class that you know points to the derived class (the "dynamic type")
    • To call the derived class' member function, you can use a static cast to DerivedClass pointer
      static_cast<DerivedClass*>(ptr)->Foo();
    • ... or a static cast to DerivedClass referencee
      static_cast<DerivedClass&>(*ptr).Foo();
  • 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