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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:47:43+00:00 2026-06-13T20:47:43+00:00

I have a C++ pure virtual base class/ interface with several implementations which are

  • 0

I have a C++ pure virtual base class/ interface with several implementations which are defined in different places in the code, and other implementations may be added later. I need a way to register the available implementations, read in (from a configuration file, user input, etc) which implementation of the interface to use, and then construct an instance of that implementation.

How can I do this in a general way (ie, table driven, not with a switch/ case statement that lists each one of implementations explicitly)? I’m having trouble figuring how to go from a runtime value indicating the type to a compile time type that I can instantiate.

Does boost have anything like this?

  • 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-13T20:47:44+00:00Added an answer on June 13, 2026 at 8:47 pm

    The standard approach involves a creational pattern called factory, which instantiates a concrete type based on a user-supplied identifier.

    If you’re using Boost already, have a look at Boost.Functional/Factory, but it’s not very difficult to build a table-based dispatching mechanism yourself. This is how you use the Boost version with run-time polymorphism:

    struct abstract
    {
        virtual ~abstract() = default;
        virtual void f() = 0;
    };
    
    struct concrete1 : abstract
    {
        virutal void f() override;
    };
    
    struct concrete2 : abstract
    {
        virutal void f() override;
    };
    
    typedef boost::function<abstract*()>  abstract_factory;
    
    int main()
    {
        // Here, each factory refers to a function, although sometimes
        // people use the term factory to mean the dispatch table itself.
        std::map<std::string, abstract_factory> factories;
        factories["foo"] = boost::factory<concrete1*>();
        factories["bar"] = boost::factory<concrete2*>();
    
        // Create a new concrete object.
        auto foo = factories["foo"]();
    
        // vtable dispatch "concrete1".
        foo->f();
    
        ...
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say we have an abstract base class IBase with pure virtual methods (an interface).
Basically, I have a pure virtual class Base, and a concrete class Derived which
I have a base class, Primitive , from which I derive several other classes--
I have created an abstract base class, which has a pure virtual method with
I have a base class 'Base', which is a pure virtual class: class Base
I have a function X() declared as PURE VIRTUAL in base class: class Base
Suppose I have a pure virtual method in the base interface that returns to
my question is rather simple, if you have an pure virtual class (interface) but
why if we have pure virtual assignment operator in a base class, then we
The pure virtual destructor in base class should have a definition. Otherwise compiler will

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.