I would like to initialize 2 classes(say Class ARegister, Class BRegister) that registers some values(A,B). I want to initialize these two classes from a super(?) class( say Class RegisterALL).
Ex: RegisterALL class will initialize ARegister and BRegister. So anybody using the module need not create objects of ARegister and BRegister individually, instead they can create object of RegisterALL.
I would like to do all these registrations in the constructor. So all that needs to be done is to create objects of the RegisterALL class and the registrations happens automatically.
We do not use exceptions in our project. So If there is some error in the registration then it is not possible to know.
Should I have separate member function that will do the registration or let the constructor do the registration.
I am a newbie to OO design. I feel something is wrong with the design, it will be helpful if you can point to the right approach.
the best way do to this is using CRTP (curiously recurring template pattern), the derived classes ARegister and BRegister pass themselves as template arguments to the base class RegisterALL.
It will look like this:
Now the initialization of the static variable
m_tempinCRTPRegisterAllpushes a creator function for each derived type ontoRegisterAll‘s list. It is generally not very good design to haveRegisterAllknow about all the derived classes (it isn’t very extensible). This way the actual creation method can be implemented in each derived class and the creator function will be automatically registered inRegisterAll. One of the main advantages of CRTP is that the derived classes don’t need to know how to register themselves to the base class’ list, it is done for them. As for error handling that too can be implemented in each derived class’ creator functions. There are better ways to handle the id issue but I don’t want to get into that here.If you want a simpler method I suggest reading about the Factory design pattern.