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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:13:03+00:00 2026-05-26T01:13:03+00:00

Is there a way to instantiate objects from a string holding their class name?

  • 0

Is there a way to instantiate objects from a string holding their class name?

I got a problem basically the same with the above question. But I need to instantiate a class with some parameters. different class constructor name may require different number of variables and the type of each variable may differ either. And because the class contains constant variables so you could not new it with new T() and then set the parameter to the correct value. the “class name”-“constructor” map seems not suitable for my needs.

Any alternatives?

  • 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-05-26T01:13:04+00:00Added an answer on May 26, 2026 at 1:13 am

    I’m going to preface this by saying, maybe C++ isn’t the right language for whatever you’re trying to accomplish. The more you try to control your program via external data, the closer you get to full on scripting. And there are lots of more powerful options when that is your goal.

    That said, sure, it is possible, although not as easily. Two ways come immediately to mind. The first is to require all applicable types to have a constructor that accepts a std::string. This constructor will be responsible for its own parsing.

    template<typename T> Base * createInstance(const std::string& s) { return new T(s); }
    typedef std::map<std::string, Base*(*)(const std::string&)> map_type;
    //...and similar changes as needed
    

    If you don’t want to change your type definitions, or this is otherwise unacceptable [perhaps your types already have such a constructor?], get rid of the templated createInstance method, and provide individual versions for each type you are interested in. This function does the parsing, and calls the appropriate constructor.

    map["derivedA"] = createDerivedA;
    map["derivedB"] = createDerivedB;
    

    A third option might be possible using variadic templates [or boost-esque variadic-like templates] that would bring you back to the original simplicity.

    template<typename T, typename... Args>
    Base* create(std::string) {
      //somehow pass the string to a generic function that packs the arguments into a tuple
      //then somehow unpack that tuple and pass the bits to the constructor
    }
    
    map["derivedA"] = create<derivedA, int, double, std::string>;
    

    However, I have no idea how to pull that off, or even if it is possible.

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

Sidebar

Related Questions

Duplicate: Is there a way to instantiate objects from a string holding their class
Is there a way to create a new class from a String variable in
In C#, is there a way to instantiate an instance of a class without
is there way how to get name ov event from Lambda expression like with
This is similar to Can PHP instantiate an object from the name of the
Is there any way to give instructions directly to the parser and lexar from
Is there a simple way to retrieve a filtered list of files from an
My questions correspond to the answer from Johannes in Is there a way to
I would like to instantiate an object from its Class object, using the constructor
Is there any way (utilizing Reflection I hope) that I can make an instantiated

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.