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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:22:55+00:00 2026-06-14T07:22:55+00:00

My program reads from a configuration file and instantiates several classes with the contents

  • 0

My program reads from a configuration file and instantiates several classes with the contents of two specific sections for each time.

To initialize Voice class, I’d call:

initializeDomain ("voice", voice_config, voice_config_fields);

and Voice should be initialized as:

Voice voice ( config, config_fields );

To do so, I programmed the following function:

void initializeDomain (string dom, map<string, string> & config, map<string, string> & config_fields)
{
    if ( dom == "voice" ) {
        Voice voice ( config, config_fields );
        return voice;
    } else if ( dom == "account" ) {
        Account account (config, config_fields);
        return account;
    }
}

This is obviously not working, as the return type is variable, depending on the class that is instantiated. So, I tried to build a template that could address this need:

template <typename T>
T initializeDomain (string dom, map<string, string> & config, map<string, string> & config_fields)
{
    if ( dom == "voice" ) {
        T instantiated ( config, config_fields );
    } else if ( dom == "account" ) {
        T instantiated ( config, config_fields );
    }
    return instantiated;                                                          }
}

But it does not work either. How can I instantiate different classes in the template?

  • 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-14T07:22:56+00:00Added an answer on June 14, 2026 at 7:22 am

    This looks like classic application for Factory Design Pattern. Allocate instances dynamically and return by [smart] pointer:

    class Base {};
    class DerivedOne : public Base {};
    class DerivedTwo : public Base {};
    
    typedef std::unique_ptr<Base> BasePtr;
    
    BasePtr createFoo( /* arguments */ ) {
        BasePtr ptr; // initialized to nullptr
        if ( ... ) ptr.reset( new DerivedOne );
        else if ( ... ) ptr.reset( new DerivedTwo );
        else ...
    
        return ptr;
    }
    

    Edit 0:

    The usage for above setup is for polymorphic types, i.e. when you have a common interface to different implementations, so you would invoke virtual methods via pointer, like:

    BasePtr base = createFoo( /* args */ );
    base->callVirtualFunction( /* args */ );
    

    What you are talking about is a bit strange. C++ is strongly-typed language, i.e. you can’t store instances of unrelated types into same variable. The simple case you are describing looks like this:

    std::string value = getInputSomewhere();
    
    if ( value == "voice" ) {
        Voice v( /* ctor args */ );
        // do your voice stuff here
    } else if ( value == "account" ) {
        Account a( /* ctor args */ );
        // do your account stuff here
    } else {
        // ...
    }
    

    So I don’t really see what you are trying to accomplish with that template.

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

Sidebar

Related Questions

I have a program that reads server information from a configuration file and would
I have a program that reads from a file that grabs 4 bytes from
My SSIS program reads as input from a .csv file. The file has about
I have a program that reads data from a file line-by-line. I would like
I have a program that reads input from a file. I am trying to
Bassicly im creating a program that reads information from an xml file into a
I want to have a program that reads metadata from an MP3 file. My
I need a program that reads from multiple files and displays a report based
When running a Python program that reads from stdin, I get the following error:
Below is the program that I wrote. /******************************************************************************* * This program reads EOF from

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.