static counter in c++

2020-06-12 03:08发布

I'm trying to create a Data class whose objects each hold a unique ID.

I want the 1st object's ID to be 1, the 2nd to be 2, etc. I must use a static int, but all the objects have the same ID, not 1, 2, 3...

This is the Data class:

class Data
{
private:
   static int ID;
public:
   Data(){
   ID++;
   }
};

How can I do it so the first one ID would be 1, the second would be 2, etc..?

5条回答
Root(大扎)
2楼-- · 2020-06-12 03:39

If the ID is static, then it will have the same value for all class instances.

If you want each instance to have sequential id values, then you could combine the static attribute with a class variable, like this:

class Data
{
private:
   static int ID;
   int thisId;
public:
   Data(){
   thisId = ++ID;
   }
};

int Data::ID = 0;

If the application will be multi threaded, then you'll have to synchronize it with something like a pthread mutex.

查看更多
Bombasti
3楼-- · 2020-06-12 03:44

This:

class Data
{
private:
   static int ID;
   const int currentID;
public:
   Data() : currentID(ID++){
   }
};

Besides a static counter, you also need an instance-bound member.

查看更多
时光不老,我们不散
4楼-- · 2020-06-12 03:47

Initialization of static variable within a function is allowed so a solution can be something like this

 class Data
 {
    private:
    static int ID ()
    {
       static int ID = 0;
       return ID ++;
    }
    int myId;

    public:
    Data(): myId (ID ())
    {      
    }
 };
查看更多
疯言疯语
5楼-- · 2020-06-12 03:57

Each instance of Data needs its own non-static member variable that stores its ID. A static variable can be used to store the last used ID which would be incremented in the constructor of Data.

Instead of a static counter, which is not thread-safe, consider using boost's uuid:

#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

using boost::lexical_cast;
using boost::uuids::uuid;
using boost::uuids::random_generator;

std::string id_ = lexical_cast<std::string>((random_generator())());
查看更多
太酷不给撩
6楼-- · 2020-06-12 04:02

where is the instance(non static) id here? you need to declare a new instance ID field like this

int m_ID;

then in your constructor do

Data(){m_ID = ::InterlockedIncrement(&ID);}

in an interlocked or other thread-safe way

查看更多
登录 后发表回答