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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:03:34+00:00 2026-06-09T21:03:34+00:00

Long introduction, question is at the end: Assume I have a base class that’s

  • 0

Long introduction, question is at the end:


Assume I have a base class that’s creating an interface

class base
{
public:
  virtual ~base();
  virtual void calc( int* variables ) = 0;
}

and a few classes that are inherited that do the work (only two are shown here):

class add : public base
{
  const int a, b, c;
public:
  add( int a_, int b_, int c_ ) : a(a_), b(b_), c(c_) {}
  void calc( int* variables ) 
  {
     variables[a] = variables[b] + variables[c];
  }
}

class inc : public base
{
  const int a;
public:
  inc( int a_ ) : a(a_) {}
  void calc( int* variables ) 
  {
     variables[a]++;
  }
}

and finally some code that’s using this construct:

base* task[2];
task[0] = new add( 0, 1, 2 );
task[1] = new inc( 3 );
int data[4];

/* ... */

for( int i = 0; i < 2; i++ )
  task[i]->calc( data );

That’s working so far – but it’s defining my tasks during compile time. This should be changed to runtime by parsing an input file. Assume the parsing has been done and in std::string variable command is the object type (like add or inc) and in a std::vector<int> params are the parameters for the constructor.

Now I could have a long list of

if( command.compare( "add" ) ) 
  task[end] = new add( params[0], params[1], params[2] );
else if( command.compare( "inc" ) ) 
  task[end] = new inc( params[0] );
else /... */

apart from becoming quite unreadable that’s just a linear seach. So in the spirit of Why switch statement cannot be applied on strings? I’d like to replace that linear search by a std::map (or hash map…).

So after this long introduction I can finally come to the question:


How can I define and fill a std::map so that references(?) to an object are stored in such a way that I can create late on the object dynamically out of that informations?

So with the code above I’d like to do something that might look like this in the end:

// define and fill
std::map< std::sting, ???? > lookup;
lookup["add"] = add;
lookup["inc"] = inc;

/* ... */

// use:
while( linesInConfigAvailable )
{
  /* ... parse ... */
  switch( params.size() )
  {
    case 1:
      task[end] = new lookup[command]( params[0] );
      break;
    case 3:
      task[end] = new lookup[command]( params[0], params[1], params[2] );
      break;
  }
}

PS: So far I didn’t need RTTI in my code. It’d be nice if that could stay so…

  • 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-09T21:03:36+00:00Added an answer on June 9, 2026 at 9:03 pm

    Well, you can’t. In C++, classes are not objects, they are more of an abstract construct that only exists within the compilers working data while it compiles.

    You could, however, create so called factory functions with a given signature:

    class A : public Base
    {
    public:
         static Base* Create() { return new A; }
    };
    
    class B : public Base
    {
    public:
         static Base* Create() { return new B; }
    };
    
    ...
    

    EDIT: if the “Create” functions are uniform like this, you can of course make a template.

    Then, you can store function pointers in the map:

    typedef Base* (*FactoryType)();
    std::map< std::string, FactoryType >
    
    lookup["A"] = A::Create;
    lookup["B"] = B::Create;
    

    and call them appropiately:

    task[end] = lookup[command]();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The introduction - the long and boring part (The question is at the end)
This is quite a long introduction to a simple question, but otherwise there will
Long story short, I have a substantial Python application that, among other things, does
Introduction I have a app that has multiple tables, some with and some without
Long introduction: Normally all data necessary for my web application are stored in session
Long question Is it possible to add a DOM element only if it does
Long story short, we found files promoting prescription drugs on our server that we
Long story short, I have an ASP.NET application I'm trying to debug and at
Long version: Those familiar to the standardization nightmare of the RSS-family, may know that
Long story short, I'm developing a theme template for a blog that enables you

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.