After some guidance towards factory method design, some research and trying to understand code examples. There are a few things that I still don't grasp.
I'm trying to make a layer system where each layer can load one scene at a time and have that scene be change-able.
To this effect I have created :
- a
Layer
class that all of my scenes inherit from - one of the possible scene classes that should be loadable (called
DLA
in this example).
Here goes
The Layer
class header file (Layer.h
)
#pragma once
#ifndef _LAYER
#define _LAYER
enum SceneType{
Scene_None,
Scene_Default,
Scene_DLA,
Scene_Boids
};
class Layer
{
public:
void setup();
void update();
void draw();
void changeScene(SceneType Type);
static Layer *CreateScene(SceneType Type);
Layer* scene;
private:
};
#endif
the Layer
class cpp file (Layer.cpp
)
#include "DLA.h"
#include "Layer.h"
void Layer::setup() {
// Start Scene
scene = CreateScene(Scene_DLA);
scene->setup(); // this causes the stack overflow
}
void Layer::update() {
//scene->update();
}
void Layer::draw() {
//scene->draw();
}
void Layer::changeScene(SceneType Type) {
if (scene) delete scene;
scene = CreateScene(Type);
}
Layer *Layer::CreateScene(SceneType Type)
{
switch (Type)
{
case Scene_None:
default:
return nullptr;
case Scene_DLA:
return new DLA();
}
}
And one of the scene classes, the DLA
class header file (DLA.h
)
#include "Layer.h"
class DLA: public Layer
{
public:
DLA();
void setup();
void update();
void draw();
private:
};
#endif
I've stripped out everything that wasn't important or related to my current problem (GUI setup, simulation content, etc)
I think my problem as of right now is that the return type of my CreateScene()
returns a type Layer
when I want it to return the type of the wanted simulation (in this specific case DLA
).
I've read that
A factory method is a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass.
So how do I get it to return an instance of a subclass ? Because as of right now, when I call simulation->setup();
inside the void Layer::setup()
I get a stack overflow error
Thanks