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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:42:23+00:00 2026-05-26T02:42:23+00:00

I am trying to do a simple class to unique ID conversion. I am

  • 0

I am trying to do a simple class to unique ID conversion. I am thinking about adding a static method:

class A {
  static int const *GetId() {
    static int const id;
    return &id;
  }
};

Each class would then be identified by unique int const *. Is this guaranteed to work? Will the returned pointer really be unique? Is there any better simpler solution?

I have also thought about pointer to std::type_info:

class A {
  static std::type_info const *GetId() {
    return &typeid(A);
  }
};

Is that better?

Edit:

I don’t need to use the id for serialization. I only want to identify a small set of base classes and I want all subclasses of some class to have the same id

  • 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-26T02:42:23+00:00Added an answer on May 26, 2026 at 2:42 am

    As I have noticed at least MSVC 2008 or 2010 optimizes the static variable, so that the following GetId function returns same address even for different classes.

    static int const *GetId() {
        static const int i = 0;
        return &i;
    }
    

    Therefore address of uninitialized constant static variable may not be used for identification. The simplest fix is to just remove const:

    static int *GetId() {
        static int i;
        return &i;
    }
    

    Another solution to generate IDs, that seems to work, is to use a global function as a counter:

    int Counter() {
        static int i = 0;
        return i++;
    }
    

    And then define the following method in the classes to be identified:

    static int GetId() {
        static const int i = Counter();
        return i;
    }
    

    As the method to be defined is always the same, it may be put to a base class:

    template<typename Derived>
    struct Identified {
        static int GetId() {
            static const int i = Counter();
            return i;
        }
    };
    

    And then use a curiously recurring pattern:

    class A: public Identified<A> {
        // ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to write a simple Static-class State Machine for my application to
I'm trying to develop a pretty simple (for now) wrapper class around int ,
I have a simple Class Hierarchy that I am trying to get to work
I created a simple class that shows what I am trying to do without
I am trying to compile and run a simple java class within eclipse. The
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
i am trying to compile this very simple piece of code class myList {
I'm trying to accomplish simply adding a css class to a div on alternate
I am trying a simple Extension Method example and am unable to increment or
I have a unique problem/situation here. Trying to make it as simple as possible.

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.