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

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 18: Line 18:
 
**  This requires the member function <code>SpecialMove()</code> to be added to the base class...
 
**  This requires the member function <code>SpecialMove()</code> to be added to the base class...
 
** ... and the signatures of these member functions in the derived classes to match up
 
** ... and the signatures of these member functions in the derived classes to match up
* Using <code>virtual</code> on the member function in the base class[https://youtu.be/4Vvc1YurUYA?t=3m17s 3:17]
+
* Using <code>virtual</code> on the member function in the base class [https://youtu.be/4Vvc1YurUYA?t=3m17s 3:17]
 +
** This enables the overridden functions in the child classes to be called
  
 
[https://youtu.be/cdOB_gKnJOM Tutorial 18.2]
 
[https://youtu.be/cdOB_gKnJOM Tutorial 18.2]

Revision as of 03:37, 12 October 2019

Another two-parter here, and we got the real stuff now. Virtual functions allow you to unlock the true potential of inheritance in C++. You need to know this shit.

Topics Covered

Part 1

  • How to create a virtual function
  • Using the override keyword
  • Creating a pure virtual function
  • Using a container of pointers to manage a heterogeneous collection of objects
  • virtual destructors

Part 2

  • Using inheritance and composition together
  • Basic idea of a polymorphic state machine and its application to entity behavior

Video Timestamp Index

Tutorial 18.1

  • Create a free function that calls SpecialMove() on the instances of derived classes of MemeFighter 0:23
    • This requires the member function SpecialMove() to be added to the base class...
    • ... and the signatures of these member functions in the derived classes to match up
  • Using virtual on the member function in the base class 3:17
    • This enables the overridden functions in the child classes to be called

Tutorial 18.2

  • WORK-IN-PROGRESS

Source Code

Inheritance Github Repository

Errata

  • Forgot the virtual destructor for class Weapon! (this one hurts)
  • In the children, the function signatures should be: int CalculateDamage( const Attributes& attr,Dice& d ) const override
  • Though not technically an error, it might have been a better decision to make Weapon::GetName() and Weapon::GetRank() (pure) virtual functions (this would reduce the amount of per-instance data to just the vtable ptr)

See also