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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:47:14+00:00 2026-06-05T04:47:14+00:00

I need to define a C++ class that generate unique identifiers. The idea is

  • 0

I need to define a C++ class that generate unique identifiers. The idea is that every instance of every visible class will be tagged by a unique integer value. Let’s call that the “handle” of the instance. The handles must be unique across all the instances in the system.

Some instances may be related to other ones, in which case their class defines a member to store the handle of their relative.

But not all instances actually have a relative. So I need a special handle value to stand for an “undefined” relative.

I need help to define that special instance. Here is what I tried:

class Handle {
public:
  Handle(): mValue(newValue()) {};

  static const Handle kUndefHandle(0);         // (1)

protected:
  Handle(int val): mValue(val) {};             // (2)
  int mValue;
  static int mNextValue = 1000; // start at 1000. (3)

  static int newValue() { return mNextValue++; }
};

Notes:

  • line (3) doesn’t compile (Clang 3.x). The compiler refuses the inline initialization of the static variable. The fix is easy, initialize it offline in an implementation file. So my class cannot be header-only, but I can live with that.

  • line (1) defines my special constant instance. Unfortunately, the compiler fails, saying “Expected parameter declarator”.

I also tried: static const Handle kUndefHandle = 0; but then the compiler complains “Variable has incomplete type ‘Handle'”, even if I put it in a subclass. And I can’t reopen a class in C++ like in Ruby.

I can sort of make it work by putting that const instance declaration outside of the class. I lose the class scope, but that’s a minor drawback. I can still use a namespace if I care.

However this only works if I make the constructor in line (2) public. And I don’t want that. I don’t want to let the programmer construct handles with arbitrary values.

Any suggestion?

  • 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-05T04:47:16+00:00Added an answer on June 5, 2026 at 4:47 am

    This works for me:

    Header

    class Handle {
    public:
      Handle(): mValue(newValue()) {};
    
      static const Handle kUndefHandle;
    
    protected:
      Handle(int val): mValue(val) {};
      int mValue;
      static int mNextValue;
    
      static int newValue() { return mNextValue++; }
    };
    

    Implementation

    const Handle Handle::kUndefHandle(0);
    
    int Handle::mNextValue = 1000;
    

    Initializing a static class member is actually considered to be within the scope of the class, so you can access private and protected constructors in that context.


    Note that you should make the constructor private, because there currently exists a loophole by which people could construct handles with arbitrary values: derive the class, chain to the protected constructor, then convert the resultant object to Handle. (See an example on ideone)

    class FakeHandle : public Handle
    {
    public:
      FakeHandle(int val) : Handle(val) { }
    };
    

    Now one could do:

    Handle badHandle = FakeHandle(5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate a CSS class that will help me define a page
I need to define some constant strings that will be used only by one
I need to generate something that can be used as a unique handle for
I want to define my own taglib that will use the g:datePicker to generate
I have a class that need to make some magic with every operator, like
I have a class with defined functions that need to be passed as parameters.
I need to define the constant in the module that use the method from
I need to define a Wix file component that may not exist in certain
I need to define an appender for log4net in a way that I get
I have a class that defines a CallRate type. I need to add the

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.