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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:30:12+00:00 2026-06-13T04:30:12+00:00

I am trying to write a base class, and a set of N derived

  • 0

I am trying to write a base class, and a set of N derived classes, where each each derived class has its own unique identifier, here is a simple, ‘manual’ implementation:

struct Base {
    static int id_ = 0;
};

struct Derived1 : public Base {
    static int id_ = 1;
};

struct Derived2 : public Base {
    static int id_ = 2;
};

The problem is, if I want to continue adding derived classes, I must count the number of already existing derived classes.

Things also get more complicated, because I want to use a bitset to represent the unique ID. If every derived class’s unique ID is basically just a different bit (of a common-length bitset) set to 1, it makes it very easy to perform binary AND/OR/XOR/etc operations on groups of derived classes.

The following is a incomplete and incorrect implementation of what I want

//Let DCOUNT be the number of derived classes, Ideally I shouldnt have to ever
//know/think about what it evaluates too, it should be automatic.
//But that is second priority, I would be willing to #define this to 
//some 'large' value, and not worry about it.
struct Base {
    static std::bitset<DCOUNT> id_ = generateUniqueID(); // "00000"
};

struct Derived1 {
    static std::bitset<DCOUNT> id_ = generateUniqueID(); // "00001"
};

struct Derived2 {
    static std::bitset<DCOUNT> id_ = generateUniqueID(); // "00010"
};

What is the best way to implement this? (or something like it)

  • 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-13T04:30:13+00:00Added an answer on June 13, 2026 at 4:30 am

    What about something simple like a family of functions (template) that generates and keeps an id for each type, like this:

     template<typename T>
     static std::bitset<DCOUNT> getId()
     {
         static std::bitset<DCOUNT> bitset;
         static bool bitsetCreated = false;
         if ( false == bitsetCreated )
         {
            bitset = generateUniqueID();
            bitsetCreated = true;
         }
         return bitset;
     }
    

    Afterwards you can get the IDs like this: getId < YourType > ();
    They are generated at runtime, so no problem with generateUniqueID();

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

Sidebar

Related Questions

So I'm trying to write a base class to handle classes that wrap std::vector,
The base class has this: def _management_form(self): # code here return form management_form =
My system has the following associations set up: class Book < ActiveRecord::Base has_many :
So the base project is this... I am trying to write a server application
I m trying write code that after reset set up rrpmax as 3000. It
I have a set of classes which I didn't write, and they're read only.
Here is the code for a simple arithmetic expression parser I wrote: class ArithmeticExpressionParser<T>
I am trying to write a simple utility webcontrol to display one-line messages inside
I'm trying to serialize class B as an instance of ita base class A
I'm trying to write a very simple custom configuration section for a .NET4 application.

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.