Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 11-May-16 3:54am
Comments
Kornfeld Eliyahu Peter 11-May-16 5:12am    
Why not to add a column to your database that stores the unique ID? A SID maybe...
Mohibur Rashid 11-May-16 5:54am    
That's the property of static variable. Think different
[no name] 11-May-16 7:18am    
You need to show more of your code. It is impossible to guess why you are not getting the correct answer.
Member 12516566 11-May-16 7:21am    
I cannot think of any code to add that would help clarify, unless you want me to just include the whole function I use it with.
[no name] 11-May-16 7:49am    
The only code you have shown is the single line in your question above. But it does not really give any clue as to why it does not work. We need more details in order to be able to help you. Have you used your debugger to identify where the code is going wrong?

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.
 
Share this answer
 
Your static variable should provide the next ID value. Each contact should contain its assigned (by means of the static variable) ID.
 
Share this answer
 
Comments
Member 12516566 11-May-16 6:49am    
That is what i do. holder->id is where the id of the contact is, which it is supposed to be equal to what idGen is. But when i make another contact, the id of the previous contact will be equal to the one that is being made.
CPallini 11-May-16 7:49am    
If you use the static member in the Contact class then its value is shared among all the instances of such class. You need also a instance (non-static) member of the Contact class in order to store the actual ID of each class instance.
You should use an auto-generated value within the database; see SQL AUTO INCREMENT a Field[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900