Difference between revisions of "Intermediate C++ Game Programming Tutorial 18"
From Chilipedia
(→Video Timestamp Index) |
(→Video Timestamp Index) |
||
Line 67: | Line 67: | ||
[https://youtu.be/cdOB_gKnJOM Tutorial 18.2] | [https://youtu.be/cdOB_gKnJOM Tutorial 18.2] | ||
<div class="mw-collapsible mw-collapsed"> | <div class="mw-collapsible mw-collapsed"> | ||
− | * Design choices in class hierarchy: | + | * Design choices in class hierarchy: inheritance vs. composition [https://youtu.be/cdOB_gKnJOM?t=0m43s 0:43] |
<div class="mw-collapsible-content"> | <div class="mw-collapsible-content"> | ||
** The MemeFighter game is expanded such that fighters can hold weapons | ** The MemeFighter game is expanded such that fighters can hold weapons |
Revision as of 21:56, 13 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.
Contents
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
- Creating a "Virtual Function" in the
MemeFighter
class 0:23
- Create a free function
DoSpecials()
that callsSpecialMove()
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
- Create a free function
- 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 applied the concept of "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
- Polymorphism creates the power to manage different types in a single container
- Applying Polymorphism to a container of pointers to the shared base type of different 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, as long as they all fall within the same hierarchy
- To manage gameplay, we
#include <algorithm>
- Use
std::any_of()
with a predicate (Lambda function) that tests if team members are still alive 11:26 - Use
std::random_shuffle()
to get different match-ups in every round of gameplay 11:46 - Use
std::partition()
to ensure the maximum number of live matchups 11:57 - Implementing the game battle loops 12:34
- In this case, we create two
- Applying Polymorphism to objects created on the heap (dynamic memory management) 13:46
- Create vectors with pointers to dynamically created objects on the heap
- When done, free the memory by calling
delete
on the pointers in the containers 14:18
- "Virtual Destructors": Managing destructors in base and derived classes 14:48
- Making the destructor in the parent class
virtual
ensures that the destructors of the children objects are called (it enables dynamic dispatch so the appropriate child class destructor is called) - The Construction and Destruction procedure and the order in which ojects are created/destroyed (RAII) 16:31
- Making the destructor in the parent class
- Review of termonology and concepts learned 19:49
- Design choices in class hierarchy: inheritance vs. composition 0:43
- The MemeFighter game is expanded such that fighters can hold weapons
- Chili argues why this situation best lends itself to a composition relationship: the abstract fighter class can have a pointer to an abstract weapon class (which has derived classes for various types of weapons)
- Adjusting the MemeFighter code to include the Weapon class 4:20
- Add feature where one fighter can take the other fighter's weapon 11:36
- We add a free function
TakeWeaponIfDead()
that handles the weapon exchange
- We add a free function
- Recap of advantage of the inheritance-composition construct 14:00
- It avoids having to change a fighter into a different type when exchanging weaponry
- One more real world example 14:28
- WORK-IN-PROGRESS
Source Code
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()
andWeapon::GetRank()
(pure) virtual functions (this would reduce the amount of per-instance data to just the vtable ptr)