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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:36:56+00:00 2026-05-12T00:36:56+00:00

imagine I have a bunch of C++ related classes (all extending the same base

  • 0

imagine I have a bunch of C++ related classes (all extending the same base class and providing the same constructor) that I declared in a common header file (which I include), and their implementations in some other files (which I compile and link statically as part of the build of my program).

I would like to be able to instantiate one of them passing the name, which is a parameter that has to be passed to my program (either as command line or as a compilation macro).

The only possible solution I see is to use a macro:

#ifndef CLASS_NAME
#define CLASS_NAME MyDefaultClassToUse
#endif

BaseClass* o = new CLASS_NAME(param1, param2, ..);

Is it the only valuable approach?

  • 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-12T00:36:56+00:00Added an answer on May 12, 2026 at 12:36 am

    This is a problem which is commonly solved using the Registry Pattern:

    This is the situation that the
    Registry Pattern describes:

    Objects need to contact another
    object, knowing only the object’s name
    or the name of the service it
    provides, but not how to contact it.
    Provide a service that takes the name
    of an object, service or role and
    returns a remote proxy that
    encapsulates the knowledge of how to
    contact the named object.

    It’s the same basic publish/find model
    that forms the basis of a Service
    Oriented Architecture (SOA) and for
    the services layer in OSGi.

    You implement a registry normally using a singleton object, the singleton object is informed at compile time or at startup time the names of the objects, and the way to construct them. Then you can use it to create the object on demand.

    For example:

    template<class T>
    class Registry
    {
        typedef boost::function0<T *> Creator;
        typedef std::map<std::string, Creator> Creators;
        Creators _creators;
    
      public:
        void register(const std::string &className, const Creator &creator);
        T *create(const std::string &className);
    }
    

    You register the names of the objects and the creation functions like so:

    Registry<I> registry;
    registry.register("MyClass", &MyClass::Creator);
    
    std::auto_ptr<T> myT(registry.create("MyClass"));
    

    We might then simplify this with clever macros to enable it to be done at compile time. ATL uses the Registry Pattern for CoClasses which can be created at runtime by name – the registration is as simple as using something like the following code:

    OBJECT_ENTRY_AUTO(someClassID, SomeClassName);
    

    This macro is placed in your header file somewhere, magic causes it to be registered with the singleton at the time the COM server is started.

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

Sidebar

Related Questions

I have a base class and a dozen derived classes. All but one derived
Imagine I have a bunch of classes, that can do logging, so I've created
Imagine that you have a column with a bunch of fields like this: ab
I have a bunch of Repository classes which all look a bit like the
Imagine you have class A which has code which runs as method M. And
Imagine I have a cell that I want to be red if the value
Imagine you have an entity that has some relations with other entities and you
I have a bunch of 'rowviews' that I want to put in a vertical
Let's imagine I have this class : public class FileData { public Weigth {
Imagine I have a folder, inside it are a bunch of photos from my

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.