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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:55:46+00:00 2026-05-26T20:55:46+00:00

I’m setting up a system where I can instantiate classes on the fly based

  • 0

I’m setting up a system where I can instantiate classes on the fly based on some information read in from a file. So, this has to be done on runtime. The classes in question are polymorphic and all inherit from the CBaseTheoryEnt class. What I want to do is associate an ID number with each class (with an unordered map for example). Essentially, my management class is going to look at a series of these ID numbers read in from the input file and then instantiate the appropriate classes. What would be an ideal and efficient way of associating the classes with the ID and then instantiating them based on the input?

  • 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-26T20:55:47+00:00Added an answer on May 26, 2026 at 8:55 pm

    One way to do this is to have a template function to instantiate the subclasses of CBaseTheoryEnt:

    template<typename T>
    CBaseTheoryEnt* instantiator() {
        return new T;
    }
    

    Then have a hash_map or array of these functions for each class that is derived from CBaseTheoryEnt and have the key for that class be associated with the instantiator for it. Then when you index the array or map, you get a function which, when called, will return a pointer to an instance of the appropriate class.

    For instance, if you had classes A, B, and C, and the id for A was 0, for B was 1, and C was 2, you’d have:

    typedef CBaseTheoryEnt* (*instantiator_ptr)();
    instantiator_ptr classes[] = {
        &instantiator<A>,
        &instantiator<B>,
        &instantiator<C>
    };
    

    Then use it like

    int idx = get_class_id();
    
    CBaseTheoryEnt* inst = classes[idx]();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't

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.