在C++中,如何使用单例来确保每个类都有唯一的整数ID?
我有很多 C++ 课程。
我希望每个类都具有以下内容:
static int unique_id;
同一类的所有实例都应具有相同的 unique_id;不同的类应该有不同的 unique_id。
最简单的方法似乎是通过类线程化单例。
但是,我不知道静态类成员/在 main 之前发生的事情何时被调用。
(1) 如果你有一个不涉及使用单例的解决方案,那也很好
(2) 如果你有一个给我一个 : 的解决方案,
int unique_id();
那也很好。
谢谢!
I have a bunch of C++ classes.
I want each class to have something like:
static int unique_id;
All instances of a same class should have the same unique_id; different classes should have different unique_id's.
The simplest way to do this appears to be threading a singleton through the classes.
However, I don't know what's called when for static class members / things that happen before main.
(1) if you have a solution that does not involve using singleton, that's fine too
(2) if you have a solution that gives me a :
int unique_id();
that is fine too.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
拥有一个在每次创建时递增其 ID 的类。然后将该类用作每个应该具有 ID 的对象中的静态字段。
Have a class that increments it's ID on each creation. Then use that class as a static field in each object that is supposed to have an ID.
基于 Kornel 的解决方案:
像这样使用它:
Building on Kornel's solution:
Use it like this:
实际上这与 RTTI 非常相似。为了实现 (2),可以利用 C++ 的内置 RTTI。对
*this
调用typeid
,并将typeinfo的地址作为唯一ID。缺点:a)ID 不固定(重新编译会改变它们),b)信息仅在给定类实例的情况下可用,c)它很难看。
你为什么想要这个?
Actually that's very similar to RTTI. To achieve (2), C++'s buildin RTTI can be exploited. Call
typeid
on*this
, and take the address of the typeinfo as unique ID.Conss: a) IDs aren't be fixed (recompile would change them), and b) the information is only available given an instance of the class, c) it's ugly.
Why do you want this?
C++ 已内置此功能。
您可以使用
typeid
运算符返回type_info
类。type_info:name()
将返回类的(唯一)名称。C++ has this already built in.
You can use the
typeid
operator to return atype_info
class. Thetype_info:name()
will return the (unique) name of the class.首先,为什么?无论如何,您都可以轻松地手动设置 ID:
然后
,当然,您必须手动跟踪每个类的 ID;此时,我会问原来的问题:为什么?
First, why? In any case, you can manually set the IDs easily:
Then
Of course, you'll have to manually keep track of the IDs for each class; at this point, I'll ask the original question: why?
我最近发现了 sbi 的版本 Kornel 的解决方案非常有用。感谢你们两位提供的答案。然而,我想进一步扩展该解决方案,以便可以轻松创建多种类型的 ID,而无需为每种新类型创建单独的 id_impl 和 id_base 类对。
为此,我对 id_impl 类进行了模板化,并向 id_base 添加了另一个参数。结果封装在一个头文件中,该头文件包含在想要添加新 ID 类型的任何位置:
对于我的应用程序,我希望几个抽象基类具有与其关联的 ID 类型。因此,对于 GeneralIDbase 模板的每个实例,指定的类型为:所声明的派生类的抽象基类,以及所声明的派生类。
以下 main.cpp 是一个示例:
这段代码的输出是
我希望这有帮助!如有任何问题,请告诉我。
I have recently found sbi's version of Kornel's solution to be very useful. Thank you both for providing your answers. However, I wanted to extend the solution further so that several types of IDs can be easily created without creating a separate pair of id_impl and id_base classes for each new type.
To do this I templated the id_impl class, and added another argument to the id_base. The result is encapsulated in a header file that is included anywhere one wants to add a new ID type:
For my application I wanted several abstract base classes to have an ID type associated with them. So for each instance of the GeneralIDbase template the types specified are: the abstract base class of the derived class being declared, and the derived class being declared.
The following main.cpp is an example:
The output of this code is
I hope this helps! Please let me know of any issues.