I don’t know if what I’m proposing in this question is a bad idea or not or even reasonable to ask because I don’t have enough experience with C++ to know any better, but I digress for the sake of getting to the point of this post.
I’m currently working on a rudimentary game engine in cooperation with the SDL2 framework for my own personal small-scale game projects. This is important to note because my question makes a clear distinction between engine class
es and front-end class
es. The only difference relevant to this question is that front-end class
es exist on a per-project basis and are aware of all engine class
es whereas engine class
es are the same set of class
es for every project and are therefore not aware of any specific front-end class
es that may exist.
I have two engine class
es: Entity
and EntityFactory<typename T>
(where of course T
is only ever intended to be Entity
or some derivative of Entity
). The idea for the front-end developer is that they make a class
like Man
that derives from Entity
, that Man
class
ONLY being constructable via a call to new Man()
in EntityFactory<Man>
. To achieve construction exclusivity as I described, I could make the default Entity
constructor private
and then make EntityFactory<typename T>
a friend
of Entity
, but then Man
ALSO needs to let EntityFactory<typename T>
be his friend
(or at least let EntityFactory<Man>
be his friend
). That part I don’t mind so much since the program will not compile UNTIL Man
becomes a friend
of EntityFactory<typename T>
, the problem I have is that Man
can define any constructors he wants and then other front-end classe
s can construct a Man
without doing so through EntityFactory<Man>
. None of this would be a problem if the design didn’t require new types of Entity
to be derived from Entity
, but I really like that about the design and I don’t want it to change.
How can I, by ONLY editing engine class
es Entity
and EntityFactory<typename T>
, make sure class
es like Man
cannot define ANY new constructors that would allow Man
to be constructed by anything other than a call to new Man()
in EntityFactory<Man>
? CAN this even be done?
If what I’m asking for IS in fact impossible, I would appreciate answers as to how to achieve the concept of ‘Factory that makes mostly the same thing but front-end classes can define slight variations of the thing that the factory makes`.
Many thanks for reading! 🙂
2