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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:32:52+00:00 2026-06-16T20:32:52+00:00

I am currently trying to create a system in which I want to be

  • 0

I am currently trying to create a system in which I want to be able to assign unique family ID’s to classes at runtime. Essentially I want to be able to distinguish between classes based on a integer-value after having them registered at runtime.

The use-case for this is that this system will be used as the bureaucrazy for a component system. All classes are descendant (not necessarily direct) from a class Component; and are registered at runtime.

I want to be able to do this for several reasons:

  • Eaze and safety of extension: People will not have to modify a gigantic list in the base component system to add Components.
  • Efficiency: Lookup internally is done using this integer value so can be O(1) instead of a search lookup. As these components will makeup the bulk of the system, I prefer not to make it an instance variable. I cannot ignore this aspect as testcases have already shown that I cannot afford > O(1) removal and insertion. (A first implementation used map lookup tables)

I am looking for a compile-time checked implementation; not a contract based solution.
A contract based solution in Java would be:

interface Component {

   // Should return the value of a static variable
   int getFamilyID();
   int setFamilyID(int id);
}

class Foo implements Component {

   static int familyID = 0;

   int getFamilyID(){ return familyID; }
   int setFamilyID(int id){ familyID = id; }
}

class System {  // Singleton
   static int registeredComponents = 0;
   void register(Component c){ c.setFamilyID(registeredComponents++); }   
}

This obviously doesn’t work for two reasons:

  • I can’t specify A.getFamilyID() unless I make the variable public; usecase: c.getFamilyID() == Foo.getFamilyID(); (instead of instanceof)
  • Redundant code all over the place; each implementation of Component would need to have the copy-pasted implementation.

I thought that I could solve the issue in C++ with a template that specified a static variable, but this becomes unusable when the class is not a direct descendant of Component.

I can also not use enums within Java as they are language-specific and the amount of components would make the code for a single file humongous. (also; they would all have to be specified in a single place again)

Any help in this matter or insight in why I’m trying to “do the wrong thing”(TM) would be quite helpfull 🙂

Edit: To clarify, I want some way to ensure at compile-time the code convention of a static integer which can be set in the component class.

  • 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-16T20:32:53+00:00Added an answer on June 16, 2026 at 8:32 pm

    In C++ you can use the curiously recurring template pattern in order to avoid code repetition.

    class Component 
    {
    public:
        virtual ~Component() {}
        virtual int getFamilyId() const = 0;
    };
    
    // each instance is assigned a unique int at construction
    class FamilyId 
    { 
        static int numberOfExistingIds = 0;
        int id;
    public:
        FamilyId() : id( numberOfExistingIds++ ) {}
        int getId() const { return id; }
    };
    
    // implementation is done only in this template class
    template <typename Derived, typename Base = Component> 
    class ComponentImpl : public Base 
    {
        static FamilyId familyId; // one instance per class for unique id
    public:
        virtual int getFamilyId() const 
        { 
            assert( typeid(*this) == typeid(Derived) ); 
            return familyId.getId(); 
        }
    };
    

    Having set this up, you can easily create new classes in your Component class hierarchy.

    // first derived class, automagically implemented by template magic
    class MyGeneralComponent 
        : public ComponentImpl<MyGeneralComponent> 
    {
         /* add new methods here */ 
    };
    
    // class further down in the hierarchy are also possible, 
    // by using the second template argument. The implementation still works. 
    class MySpecificComponent 
        : public ComponentImpl<MySpecificComponent,MyGeneralComponent> 
    {
         /* add new methods here */ 
    };
    

    The assert(...) will automatically check at run-time, if you correctly derived from the template. So you will uncover bugs like

    class MySpecificComponent : MyGeneralComponent 
    {
    };
    

    at run-time. This derived class would otherwise use the same interface implementation as the direct base and use the same static variable, which would be a bug.

    You might have noticed that you don’t need to register any classes by hand. This is done via dynamic initialization of static variables before the main() function starts. So you don’t need to do anything about that. In this way you can easily implement your classes in one spot, without changing other files and without lots of code repetition – open/closed principle par excellence.

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

Sidebar

Related Questions

I am currently trying to create a content uploading system and although there are
I am currently trying to create a function which will allow me to pass
I'm currently trying to create a T-SQL, which runs through a list of deliveries
I'm currently trying to create a template in TextMate. The template is very basic:
I'm currently trying to create an email application using Rails and Sendgrid. This application
I'm currently trying to create a program that estimates location based on signal strength.
I'm currently trying to create a desktop like homepage where using can move around
I am currently trying to create an Android application that loops Audio from the
I'm currently trying to create a subclass of UIImageView in order to make it
I'm currently trying to create a list of 5 blog titles of mine, where

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.