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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:35:56+00:00 2026-05-20T01:35:56+00:00

I want to add unique ID (within a single session) to each object of

  • 0

I want to add unique ID (within a single session) to each object of a certain class. One solution is to use a factory function which increments some static counter. A simpler solution is to add this counter to the class itself, e.g.:

class fooWithUniqueId {
public:
    fooWithUniqueId() : id(next_id++) {...};        
    long id;

private:
    static long next_id = 0;
}

A flaw, however, is that the id field is public, and can be changed by the caller, thus violating its uniqueness. A traditional (well, at least in my eyes) is to make id private, and use a getter function to access it, thus:

class fooWithUniqueId {
public:
    fooWithUniqueId() : id(next_id++) {...};                
    long getId() const { return id; };

private:
    long id;
    static long next_id = 0;
}

But I’m considering a different approach. I can make id a const public class field:

class fooWithUniqueId {
public:
    fooWithUniqueId() : id(next_id++) {...};                
    const long id;

private:
    static long next_id = 0;
}

I like this way better because I don’t have to keep calling getId() each time I need the id, I can use the id as a key in a map (as copy construction correctly initializes the id of the copy object). One disadvantage I can think of is that I cannot implement assignments between fooWithUniqueId objects, although currently I do not need this feature.

  • What are the pros and cons of each approach (getter function / const field)?
  • Assuming I’m using the ‘const’ approach, is there any way to later implement an assignment operator without breaking the code?

Thanks, Boaz

  • 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-20T01:35:56+00:00Added an answer on May 20, 2026 at 1:35 am

    I can use the id as a key in a map (as copy construction correctly initializes the id of the copy object)

    What do you mean by “correctly”? The default copy constructor will copy the ID, whether it is stored in a private or a public member variable, and you will end up with two objects sharing the same ID. This might not be what you want.

    In general, you should never use public variables in C++ as it violates proper encapsulation. Always use a (inline) getter method. The only disadvantage is that you have to type a few more characters.

    I strongly suggest that you stick to best practices and use a private field with a getter function.

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

Sidebar

Related Questions

I have dbml with single table users i want add partial class for User
I want to add tracing capabilities to my messaging application. Each message I use
I want to add my own member to the StringBuilder class, but when I
how can I add to each node I add to xml a unique index
I want to use a temp directory that will be unique to this build.
I want to add a column to an existing legacy database and write a
I want to add a constant value onto an incoming bound integer. In fact
I want to add a scheduled task to a client's ASP.NET app. These posts
I want to add a mailto link on our web page. I want to
I want to add a property to my User model that returns the number

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.