如何在类中允许唯一ID(C ++) [英] How do I allow unique ID with a class (C++)

查看:117
本文介绍了如何在类中允许唯一ID(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个数据库,我正在使用静态变量来跟踪数据库中每个联系人的ID。当我尝试使用变量虽然它改变了任何其他也有id的联系人。



这是我用的静态变量代码,holder是新的联系方式被添加。

holder-> id = idGen ++;



我尝试过:



我试过谷歌搜索它但没有找到任何关于我的特定问题,以及任何事情。

I am trying to make a database, and i am using a static variable to keep track of the ID of each contact inside the database. When i try to use the variable though it changes any other contacts that also have an id.

This is the code i use with the static variable, holder is the new contact being added.
holder->id = idGen++;

What I have tried:

I have tried googling it but haven't found anything talking about my particular problem, and anything.

推荐答案

您确定需要为每个对象合成一个唯一的ID吗?



在用C ++实现的数据库中,每个对象将有两个与之关联的唯一值 - 内存中对象的地址或数据库存储在文件中的对象的偏移量。如果您的数据库是基于表的,则您有第三个唯一标识符 - 对象从表的开头的偏移量。其中任何一个都可用于明确识别对象并存储在其他对象中。
Are you sure you need to synthesise a unique ID for each object?

In a database implemented in C++ each object is going to have two unique values already associated with it - the address of the object in memory or the offset of the object within the file the database is stored in. If your database is table based you've got a third unique identifier - the offset of the object from the start of the table. Any of these can be used to unambiguously identify an object and stored in other objects.


您的静态变量应提供 next ID 价值。每个联系人都应包含其分配的(通过静态变量) ID
Your static variable should provide the next ID value. Each contact should contain its assigned (by means of the static variable) ID.


您应该在其中使用自动生成的值数据库;请参阅 SQL AUTO INCREMENT a Field [ ^ ]。
You should use an auto-generated value within the database; see SQL AUTO INCREMENT a Field[^].


这篇关于如何在类中允许唯一ID(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆