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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:27:54+00:00 2026-05-13T15:27:54+00:00

I have a bunch of C++ classes. I want each class to have something

  • 0

I have a bunch of C++ classes.

I want each class to have something like:

static int unique_id;

All instances of a same class should have the same unique_id; different classes should have different unique_id’s.

The simplest way to do this appears to be threading a singleton through the classes.

However, I don’t know what’s called when for static class members / things that happen before main.

(1) if you have a solution that does not involve using singleton, that’s fine too

(2) if you have a solution that gives me a :

int unique_id(); 

that is fine too.

Thanks!

  • 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-13T15:27:55+00:00Added an answer on May 13, 2026 at 3:27 pm

    Have a class that increments it’s ID on each creation. Then use that class as a static field in each object that is supposed to have an ID.

    class ID
    {
        int id;
    public:
        ID() {
            static int counter = 0;
            id = counter++;
        }
    
        int get_id() {  return id; }
    };
    
    class MyClass
    {
        static ID id;
    public:
        static int get_id() 
        {
            return id.get_id();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a bunch of classes that look something like... class Foo{
imagine I have a bunch of C++ related classes (all extending the same base
I have a bunch of Repository classes which all look a bit like the
I have a class that should look something like this: class Family_Type1 @people =
I have a bunch of classes that each have a property called Sequence. This
I have a parent class: CMove I have a bunch of child classes that
Maybe I'm going about this all wrong. I have a bunch of classes that
I have a bunch of repository classes which I want to unit-test using Visual
The question states what I want to do. I have a bunch of classes
I have a bunch of classes I want to rename. Some of them have

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.