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

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
 
(6 intermediate revisions by 2 users not shown)
Line 11: Line 11:
 
[https://youtu.be/g-NGBFCn3co Tutorial 19]
 
[https://youtu.be/g-NGBFCn3co Tutorial 19]
 
<div class="mw-collapsible mw-collapsed"><br />
 
<div class="mw-collapsible mw-collapsed"><br />
* Using <code>dynamic_cast<new-type>(expression)</code> to determine to what derived type a pointer to a polymorphic base type is actually pointing [https://youtu.be/g-NGBFCn3co?t=54s 0:54]
+
* Using <code>dynamic_cast<DerivedType>(pBaseType)</code> to determine to what derived type a pointer to a polymorphic base type is actually pointing [https://youtu.be/g-NGBFCn3co?t=54s 0:54]
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
 
** A dynamic cast can be used on pointers and on references
 
** A dynamic cast can be used on pointers and on references
Line 34: Line 34:
 
* Overview of different types of casts [https://youtu.be/g-NGBFCn3co?t=9m30s 9:30]
 
* Overview of different types of casts [https://youtu.be/g-NGBFCn3co?t=9m30s 9:30]
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
** static_cast
+
** <code>static_cast<>()</code>
 
*** converts types (int->float etc.)
 
*** converts types (int->float etc.)
 
*** casts pointers/references within an inheritance hierarchy (no checking of the validity of the cast!)
 
*** casts pointers/references within an inheritance hierarchy (no checking of the validity of the cast!)
** dynamic_cast
+
** <code>dynamic_cast<>()</code>
 
*** casts pointers/references within an inheritance hierarchy (dynamic check!)
 
*** casts pointers/references within an inheritance hierarchy (dynamic check!)
 
*** requires that the type have at least one virtual function
 
*** requires that the type have at least one virtual function
 
***  if check fails, nullptr for ptr* cast, throw exception for ref& cast
 
***  if check fails, nullptr for ptr* cast, throw exception for ref& cast
** reinterpret_cast
+
** <code>reinterpret_cast<>()</code>
 
*** reinterpret pointers/references as pointing to some other type (no check, no limit!) (only really safe for char*)
 
*** reinterpret pointers/references as pointing to some other type (no check, no limit!) (only really safe for char*)
 
*** reinterpret pointer as an integral value
 
*** reinterpret pointer as an integral value
** const_cast
+
** <code>const_cast<>()</code>
 
*** remove constness
 
*** remove constness
** c-style cast (int) etc.
+
** <code>(...)...</code> c-style cast (int) etc.
 
*** can do all of the above, even if you don't mean to<br />
 
*** can do all of the above, even if you don't mean to<br />
 
***<code>const Base* pBase = &obj;</code> <br />
 
***<code>const Base* pBase = &obj;</code> <br />
Line 62: Line 62:
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
 
** Get the type name of an object by calling <code>typeid(obj).name()</code>
 
** Get the type name of an object by calling <code>typeid(obj).name()</code>
** You can't use typeid in switch statements, but you can use them in map class to get similar functionality
+
** You can't use typeid in switch statements, but you can use them in a map container class to get similar functionality
 
** typeid does have an overhead, like dynamic_cast, so avoid its use in super performance critical applications
 
** typeid does have an overhead, like dynamic_cast, so avoid its use in super performance critical applications
 
</div>
 
</div>

Latest revision as of 21:53, 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

[Expand]
  • Using dynamic_cast<DerivedType>(pBaseType) to determine to what derived type a pointer to a polymorphic base type is actually pointing 0:54
  • Using static_cast<>() to convert pointers within an inheritance hierarchy 6:20
  • Using const_cast<>() to remove a const from a reference or pointer (yes, you read that right) 7:27
  • Overview of different types of casts 9:30
  • Using RTTI (Runtime Type Information) 13:01
  • The type_info class 15:05

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