I am trying to make a class ShapeManager
which is instantiated only once, and have a base class Shape
(and all of its children) have access to it. This would result in a structure that looks like this:
ShapeManager
Shape
└Square
└Triangle
└Circle
#include "Shape.h" // so that i can use Shape in the vector
// This is instantiated once in main
class ShapeManager {
public:
typedef std::vector<std::unique_ptr<Shape>> VecShape;
typedef std::default_random_engine DRE;
private:
VecShape& m_shapes;
b2World& m_world;
DRE& m_gen;
public:
ShapeManager(VecShape& vectorShapes, b2World& world, DRE& gen)
: m_shapes(vectorShapes), m_world(world), m_gen(gen) {}
VecShape& getShapeList() { return m_shapes; }
b2World& getWorld() { return m_world; }
DRE& getDRE() { return m_gen; }
};
#include "ShapeManager.h" // so that i can access it's member functions
class Shape {
protected:
std::string m_name;
public:
Shape() : m_name("Shape") {}
std::string getName() { return m_name; }
};
I simplified my code a lot to keep it relevant to my question. If it's unclear what i'm asking i will try to explain it further!
Basically i just want to have a class that manages my shape objects & holds data that Shape's
children will use such as m_gen
and m_world
.
EDIT 1: My main motivation to creating a ShapeManager is to store a reference to m_shapes, m_world, and m_gen, but i don't want every shape object to have those 3 reference variables. That would mean creating a Circle would have to have those 3 parameters in the ctor, along with the parameters relevant to the circle.
EDIT 2: Singleton and/or Dependency Injection are the two design patterns that, when implemented, solve my issue. Here is some info on it. Thank you all!