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

  • Home
  • SEARCH
  • 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 202369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:17:02+00:00 2026-05-11T17:17:02+00:00

Duplicate: Is there a way to instantiate objects from a string holding their class

  • 0

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


Is there a (better) way in C++ to map string id to a class name. I suspect there might be a way via templates but I wasn’t able to figure out the correct way.

For example if I have multiple messages, each storing in the first byte a character id. So message A12345 would instantiate class A() message B12345 would map to class B().

I have a group of message parsers where each class can parse give message. The issue is that I have to manually generate mapping between class that parses given message (e.g. class A()) and the message id (e.g. the ‘A’).

This is what I’m doing in the code now, and I’m thinking if there’s a nice way to eliminate the switch in c++ (templates magic?):

Msg * msg;
switch(data[0])
{
    case 'A': msg = new A(); break;
    case 'B': msg = new B(); break;
    ...
}

msg->parse(data);

On the side note, most of the time the id’s are actually 2 character long so ‘A1’ ‘B1’ and so on. And I’m using the class names the same to keep track of the classes, e.g. ‘A1’ can be parsed by class A1() and so on.

NOTE: This seems like duplicate of C++: Is there a way to instantiate objects from a string holdig their class name? so I suggest to close this question.

  • 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-11T17:17:02+00:00Added an answer on May 11, 2026 at 5:17 pm

    As someone else said, it can’t be done using templates (templates are computed at compile time. But your character id is compute at runtime).

    You can use a map from id to constructor function. It boils down to this question: Instantiate objects from a String holding their class name

    I recommend you to keep it simple. If a plain switch will do it, keep it that way. If you later really need to have it extensible, you can still introduce some automatic look-up of character ids and so on.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer JQuery has a serialize() command; it works on a set… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer This seems to work when using sockets: require 'socket' host… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer Ah, yes, Word and it's lovely "smart quotes" feature. Have… May 11, 2026 at 11:47 pm

Related Questions

Duplicate: Is there a way to indicate the last n parameters in a batch
Possible Duplicate: Is there a way for MS Access to grab the current Active
having a little architectural trouble here. In C++, we have the notion of 'friends,'
Exact duplicate of Is there a way to determine whether an e-mail reaches its

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.