I'm trying to engineer this:
- 200 subclasses [ Derived Classes ]
- After a subclass is defined, I wont need to edit any other file. [ Decoupled ]
- Subclass Definition registers itself. [ Definition is Registration ]
What I could do instead:
Create a
class
[ One big one ]Create an
enum
value (or similar) for every functional variant [ Highly Coupled ]alter behaviour in a switch [ Cumbersome ]
While that would work with its own advantages, I'd like to see if something more polymorphic, decoupled, and Modular is achievable, like this:
class Base { ... };
static QList<Base> s_RegisteredClasses; // Empty
class A : public Base{ ... };
class B : public Base{ ... };
/* s_RegisteredClasses should now have A & B inside of it */
What I've tried involves static constructors
, where every base class has a different signature. The initialization works, but I wouldnt know how to register it:
class Register { public: Register() { /* Static Constructor to run any code */ } };
template<class T>
class Base { static inline Register selfRegister{} }; // `Register` initiated
static QList<Base<?>> s_RegisteredClasses; // ? prevents polymorphism
class A : public Base<A>{ ... };
class B : public Base<B>{ ... };
/* Both A & B run `Register()` constructor,
but how do you add them to a list? */
The self initialization works, registration doesn't.
Should I consider a QList<void *>
instead?
The other variant looks like this, with the base class having one signature:
class Register { public: Register() { /* Static Constructor to run any code */ } };
template<class T>
class Base { static inline T selfRegister{} }; // Register initiated only once.
static QList<Base<Register>> s_RegisteredClasses; // but I can now polymorph
class A : public Base<Register>{ ... };
class B : public Base<Register>{ ... };
/* How can I self initialize A & B, adding them to s_RegisteredClasses ? */
I am close to giving up, but I haven't tried registering function pointers
yet.
Is a text Macro the appropriate solution here, or that there is another paradigm I am overlooking?
Context
I am designing a Card Game where the instructions are printed in a Pseudo C++ code. I am hoping a subclass would represent a Card looking like this:
Card
class. (In other words, "behaviors driven by data.") You can initialize these 200 instances inside C++ code, no need for a separate language.