Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:40:16+00:00 2026-06-05T03:40:16+00:00

I would like to initialize 2 classes(say Class ARegister, Class BRegister) that registers some

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:40:17+00:00Added an answer on June 5, 2026 at 3:40 am

    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:

    class RegisterAll
    {
    public:
        template<class DerivedType>
        DerivedType *createType()
        {
            RegisterAll *r = (*(m_creators[DerivedType::id]))();
            return dynamic_cast<DerivedType *>(r); //static_cast will work too if you didn't make a mistake
        }
    protected:
        static std::map<int,RegisterAll *(*)()> m_creators;
    };
    
    std::map<int,RegisterAll *(*)()> RegisterAll::m_creators = std::map<int,RegisterAll *(*)()>();
    
    template<class Derived>
    class CRTPRegisterAll : public RegisterAll
    {
    public:
        static bool register() 
        {
            RegisterAll::m_creators.push_back(std::make_pair(Derived::id,Derived::create);
            return true;
        }
    
    private:
        static bool m_temp;
    };
    
    template<class Derived>
    bool CRTPRegisterAll<Derived>::m_temp = CRTPRegisterAll<Derived>::register();
    
    class RegisterA : public CRTPRegisterAll<RegisterA>
    {
    private:
        static RegisterA *create() 
        {
            //do all initialization stuff here
            return new RegisterA;
        }
    
    public:
        static const int id = 0;
    };
    

    Now the initialization of the static variable m_temp in CRTPRegisterAll pushes a creator function for each derived type onto RegisterAll‘s list. It is generally not very good design to have RegisterAll know 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 in RegisterAll. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have two classes A and B. class A { private:
I would like to define and initialize some variables in web.xml and the access
I would like to know: how can i initialize class object with parameters? I've
I would like to initialize a class, then call an attribute which was set
I would like to initialize boost::random::discrete_distribution with a double[] like this: boost::random::discrete_distribution<>* distribution(double* _distr)
I have a Visual Studio 2008 C++03 application where I would like to initialize
Here is part of my code. I would like to initialize only the arraylist[0]
where would I place additions to stdlib classes in a rails project? Let's say
My problem here is that I would like to pass an object to a
I have the following two classes: public class User { public Integer userId; //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.