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

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 32: Line 32:
 
* Applying Polymorphism to a container of pointers to the shared base type of derived class objects [https://youtu.be/4Vvc1YurUYA?t=9m46s 9:46]
 
* Applying Polymorphism to a container of pointers to the shared base type of derived class objects [https://youtu.be/4Vvc1YurUYA?t=9m46s 9:46]
 
** In this case, we create two <code>std::vector<MemeFighter*></code>s to enable team fights
 
** In this case, we create two <code>std::vector<MemeFighter*></code>s to enable team fights
 +
** These vectors contain pointers to the base type, this allows us to have a single vector that can manage objects of different types
 
** To manage gameplay, we apply <code>std::random_shuffle()</code>,  
 
** To manage gameplay, we apply <code>std::random_shuffle()</code>,  
 
* WORK-IN-PROGRESS
 
* WORK-IN-PROGRESS

Revision as of 20:48, 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

  • Creating a "Virtual Function" in the MemeFighter class 0:23
    • Create a free function DoSpecials() that calls SpecialMove() on the instances of the derived classes of MemeFighter
    • 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
  • Enabling "Dynamic Dispatch" by using virtual on the member function in the base class 3:17
    • This enables the overridden functions in the child classes to be called
    • When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function
  • Applying the override keyword to increase code safety 5:17
    • Tells the compiler that you are intending to override something virtual in a base class
  • Making a base class member function "Pure Virtual" using virtual void Func(...) = 0; 7:08
    • This makes the base class an Abstract Class which cannot be instantiated
    • It enforces that the virtual function is overridden in all sub-classes
    • Whether you make a pure virtual function or whether you supply a default implementation depends on your situation 8:47
  • We have now created "Polymorphism" 9:16
    • C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the underlying type of object that invokes the function
  • Applying Polymorphism to a container of pointers to the shared base type of derived class objects 9:46
    • In this case, we create two std::vector<MemeFighter*>s to enable team fights
    • These vectors contain pointers to the base type, this allows us to have a single vector that can manage objects of different types
    • To manage gameplay, we apply std::random_shuffle(),
  • WORK-IN-PROGRESS

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