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 9202165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:11:26+00:00 2026-06-17T23:11:26+00:00

I need to port the following Java concept into C++ : Hash map holding

  • 0

I need to port the following Java concept into C++ :

Hash map holding object id key and class type value:

 Map<String, Class> _objectsBank = new HashMap<>();

Somewhere in the init method I fill the bank like this:

    _objectsBank .put("CLASS_ID_1", MyClass1.class);
    _objectsBank .put("CLASS_ID_2", MyClass2.class);
    ....

Then , later ,I construct an instance of one of the classes saved in that bank by demand.
Kind of “lazy” init :

 private MyClass initNewProg(String name) {

    MyClass instance;


    try {

        Class cl = _objectsBank.get(name);
        java.lang.reflect.Constructor co = cl.getConstructor(String.class);
        instance= (MyClass) co.newInstance(name);
        return instance;

    } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
    {
        e.printStackTrace();
        return null;
    }
 }

How would I do it in C++ ? How can I set a class type as std::map value so that I can query it later to construct an appropriate instance from it?
Is there something like this in Boost libraries?

  • 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-17T23:11:27+00:00Added an answer on June 17, 2026 at 11:11 pm

    Take a look at Is there a way to instantiate objects from a string holding their class name? and Instantiating classes by name with factory pattern, which describe the problem and multiple approaches with pros & cons of each ones. The examples cover registration of contructors for classes, which solves the issue with multiple if-statements you mentioned in @Karthik T’s solution.

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

Sidebar

Related Questions

As a user I type following command into the java console: !login <Username> <udpPort>
I need to port the following from the ASP.NET MVC 2 sourcecode from C#
I have the following web server class found here . I need to write
I have the following scenario of integrating a third party application into my java
I am attempting to port javascript code to Java. In so doing, I need
I have the following VB.net interface that I need to port to C#. C#
I am a Scala newbie and I need to port part of a java
I need to port some code from FreePascal to C. I'm a professional C
I need to port one of my old program to Windows. The current version
I have a win32 GUI (MFC) application which I need to port to a

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.