April 2017
Beginner to intermediate
394 pages
9h 16m
English
The simplest way to implement a factory method the way we want is with either a global function or a static class function. We could define a function called MakeStage outside of StateMananger that is responsible for instantiating the correct derived type based on a parameter. In this case, we will use an enum called StageType to help us choose our correct type:
//MakeStage.cpp Stage* MakeStage(StageType type) { switch(type) { case ST_Level1: return new Level1; case ST_LEVEL2: return new Level2; case ST_MainMenu: return new MainMenu; default: //Throw exception or assert } }
If we use this style of factory, our StageManager::Update function will look like this:
void StageManager::Update() { Stage* pStage = ...Read now
Unlock full access