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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:53:52+00:00 2026-05-12T08:53:52+00:00

I have the convenient object factory template that creates objects by their type id

  • 0

I have the convenient object factory template that creates objects by their type id names. The implementation is pretty obvious: ObjectFactory contains the map from std::string to object creator function. Then all objects to be created shall be registered in this factory.

I use the following macro to do that:

#define REGISTER_CLASS(className, interfaceName) \
   class className; \
   static RegisterClass<className, interfaceName> regInFactory##className; \
   class className : public interfaceName

where RegisterClass is

   template<class T, class I>
   struct RegisterClass
   {
      RegisterClass()
      {
         ObjectFactory<I>::GetInstance().Register<T>();
      }
   };

Usage

class IFoo
{
public:
   virtual Do() = 0;
   virtual ~IFoo() {}
}

REGISTER_CLASS(Foo, IFoo)
{
   virtual Do() { /* do something */ }
}

REGISTER_CLASS(Bar, IFoo)
{
   virtual Do() { /* do something else */ }
}

Classes are defined and registered in factory simultaneously.

The problem is that regInFactory... static objects are defined in .h files, so they will be added to every translation unit. The same object creator will be registered several times, and, more important, there will be a lot of redundant objects with static storage duration.

Is there any way to do such elegant registration (not to copy/paste class and interface names), but do not spread redundant static objects around the globe?

If a good solution needs some VC++ specific extensions (not conforming to C++ standard), I will be OK with that.

  • 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-12T08:53:52+00:00Added an answer on May 12, 2026 at 8:53 am

    So you want to put variables definitions in header file? There is a portable way: static variables of template classes. So we get:

    template <typename T, typename I>
    struct AutoRegister: public I
    {
        // a constructor which use ourRegisterer so that it is instantiated; 
        // problem catched by bocco
        AutoRegister() { &ourRegisterer; } 
    private:
        static RegisterClass<T, I> ourRegisterer;
    };
    
    template <typename T, typename I>
    RegisterClass<T, I> AutoRegister<T, I>::ourRegisterer;
    
    class Foo: AutoRegister<Foo, IFoo>
    {
    public:
        virtual void Do();         
    };
    

    Note that I’ve kept the inheritance of IFoo non virtual. If there is any risk of inheriting from that class multiple times, it should be virtual.

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

Sidebar

Ask A Question

Stats

  • Questions 168k
  • Answers 168k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can call a function, if you like: autocmd Filetype… May 12, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer Assuming getLoc() is static, try using the following: using Me… May 12, 2026 at 1:46 pm
  • Editorial Team
    Editorial Team added an answer The most compatible way to do this would be to… May 12, 2026 at 1:46 pm

Related Questions

I'm working on a project where there is a lot of external service messaging.
I am coding my first object factory, and I am wondering if there any
I wrote an application which allows people to contribute plugins to extend the functionality.
I have a C library with numerous math routines for dealing with vectors, matrices,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.