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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:07:04+00:00 2026-05-31T20:07:04+00:00

I’m a beginner programmer, and I was wondering… I need a unique ID for

  • 0

I’m a beginner programmer, and I was wondering…

I need a unique ID for my object in a game

so I thought I define a global class that holds the global id, and when I call the constructor of an object it will be incremented, while calling the destructor of that object it decrements it.

Is it true that for every constructor call there will be a corresponding destructor?

class id_holder{
private:
    int id_count;

public:
    id_holder() {id_count = 0 ; std::cout<< "unique_id: I'm constructed"<<std::endl;}

    ~id_holder(){std::cout<<"unique_id: I'm destructed"<<std::endl;}

    void increment_id(){id_count ++;};
    void decrement_id(){id_count --;};

    int get_id() {return id_count;};

};

class BaseObject {
protected:
    void increment_id();
    void decrement_id();
public:
    virtual ~BaseObject(){decrement_id();}
    BaseObject();
    BaseObject(V2 pos, ObjectType idobj, double s, Color col);
};

BaseObject::BaseObject(){   
    //stuff
    increment_id();
    unique_id = bObj_id_counter.get_id();
}


BaseObject::BaseObject(V2 pos, ObjectType idobj, double s, Color col){
    //stuff
    increment_id();
    unique_id = bObj_id_counter.get_id(); 
}

void BaseObject::increment_id(){
    bObj_id_counter.increment_id();
}

void BaseObject::decrement_id(){
    bObj_id_counter.decrement_id();
}

side question… is it possible to compare the memory adresses (this should be a unique id enough) of the objects instead of using an id_holder?

  • 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-31T20:07:05+00:00Added an answer on May 31, 2026 at 8:07 pm

    This is typically called “reference counting” and it has a wide range of uses. Yes it is possible to do, but yours won’t quite work correctly just yet.

    1. If you want a global unique identifier for all your objects, you would need to have one id held statically, and then use the curiously recurring template pattern to ensure that this id is global only for specific types. Then each type gets a copy of the global id during construction.
    2. You need to increment the counter in the copy-constructor
    3. Since increment_id() and decrement_id() are called by the id_holder class, there is no reason to call them again in your BaseObject constructor/destructor.
    4. It will not work in the presence of multithreaded access, you need to find a way to atomically set the “reference count”.

    This is a relatively elegant solution that allows for you to do things like constant-time lookups for an item given its ID. For example:

    Warning pseudo-code (not compiled or tested)

    template <class T>
    class counter
    {
       static size_t global_id_;
       size_t id_;
    
    public:
        counter() : id_(global_id_++) {}
        counter(const counter&) : id_(global_id++) {}
        ~counter() {}
    
        counter& operator=(const counter&)
        {
          // left as an exercise for the reader
          // as what to do in this case is highly 
          // dependent upon the application
        }
    };
    
    class counted_object : public counter<counted_object>
    {
       // stuff
    };
    

    Now you could hold a lookup table of counted_objects stored in a vector for very quick lookups:

    std::vector<counted_object*> lookup;
    counted_object* o416 = lookup[416];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I need to clean up various Word 'smart' characters in user input, including but

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.