Base class with member parameterized on type of extending class
Jacob Carlborg via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Oct 20 04:24:05 PDT 2014
On 2014-10-20 12:27, rcor wrote:
> I would, as I need to keep track of the current scene in a variable
> somewhere:
>
> Scene _currentScene; // problematic if Scene is a template
If the state machine doesn't need to be exposed you can create base
class for Scene which is not templated:
abstract class Scene {} // As it is now minus the state machine
abstract class ConcreteScene (T) : Scene
{
private StateMachine!T _stateMachine;
// other code that need access to _stateMachine
}
class MainMenu : ConcreteScene!(MainMenu) {}
Scene _currentScene = new MainMenu;
"ConcreteScene" might not be the best name of an abstract class.
> I could just declare the StateMachine separately in every Scene, but
> that seems like a lot of duplicate code (I then repeat the same code for
> updating the state machine, ect.)
Or you could use a template mixin:
template StateMachineMixin (T)
{
private StateMachine!T _stateMachine;
// other code that need access to _stateMachine
}
class MainMenu : Scene
{
mixin StateMachineMixin!(typeof(this));
}
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list