Fast/easy removal from container classes? (C++)
Started by poita, Apr 24 2006 01:49 PM
12 replies to this topic
#1
Posted 24 April 2006 - 01:49 PM
Alright, suppose the following situation. You have a person class and you also have a company class. A company is a group of people so you would have a member variable list containing pointers to all the persons in the company.
Adding people to a companies list is easy. However, say a person dies (or whatever) and I need to delete him. How can I quickly and easily remove this person from every list that he is in? Simply deleting him will result in a bunch of bad pointers in lists.
Thanks in advance
Adding people to a companies list is easy. However, say a person dies (or whatever) and I need to delete him. How can I quickly and easily remove this person from every list that he is in? Simply deleting him will result in a bunch of bad pointers in lists.
Thanks in advance
#2
Posted 24 April 2006 - 02:00 PM
And you don't want to store a list of companies in the class Person itself because...?
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.
-
Currently working on: the 3D engine for Tomb Raider.
#3
Posted 24 April 2006 - 02:10 PM
.oisyn said:
And you don't want to store a list of companies in the class Person itself because...?
Yes, that would work.
But now lets say, that people can be in other lists other than companies (eg. clubs, families, social groups, whatever) and what if the comapany (or whatever) gets removed too?
#4
Posted 24 April 2006 - 02:39 PM
Store pointers booth ways, and inform the object in the other end if you are about to delete person/company.
You also probably want to create a base class for collections of people. That way you don't need add specialized code for companies, families, clubs, etc.
You also probably want to create a base class for collections of people. That way you don't need add specialized code for companies, families, clubs, etc.
#5
Posted 24 April 2006 - 02:56 PM
Or you might want to use a standard sql database for all this..
#6
Posted 24 April 2006 - 03:13 PM
geon said:
Store pointers booth ways, and inform the object in the other end if you are about to delete person/company.
You also probably want to create a base class for collections of people. That way you don't need add specialized code for companies, families, clubs, etc.
You also probably want to create a base class for collections of people. That way you don't need add specialized code for companies, families, clubs, etc.
Ok thanks :yes:
#7
Posted 24 April 2006 - 05:52 PM
juhnu said:
Or you might want to use a standard sql database for all this..
Not an option, since this is obviously an exercise in C++ class design. But, yes, an SQL database would be the (only?) rational solution.
#8
Posted 29 April 2006 - 01:47 PM
If all your containers have regular "update" functions, then in order to kill a person, you simply flag him as "dead" (but dont delete the object yet) and all containers detect and remove any dead persons during the update. Once all containers have updated, and thus have eliminated their references to that person, the dead person itself can be safely buried er... deleted. :)
How to know if all containers have updated?
a) a reference counter in the person that lists how many containers it is held into. Containers increase that reference counter in the person when the person is added to a container, and decrement the reference counter when the person is removed from a container.
b) guaranteed maximum amount of time for all containers to have run an update. This may seem quirky and error-prone, but is in fact fairly typical in one way or another. Just make sure you get the "guaranteed" part right.
How to know if all containers have updated?
a) a reference counter in the person that lists how many containers it is held into. Containers increase that reference counter in the person when the person is added to a container, and decrement the reference counter when the person is removed from a container.
b) guaranteed maximum amount of time for all containers to have run an update. This may seem quirky and error-prone, but is in fact fairly typical in one way or another. Just make sure you get the "guaranteed" part right.
#9
Posted 29 April 2006 - 02:05 PM
Jare said:
If all your containers have regular "update" functions, then in order to kill a person, you simply flag him as "dead" (but dont delete the object yet) and all containers detect and remove any dead persons during the update. Once all containers have updated, and thus have eliminated their references to that person, the dead person itself can be safely buried er... deleted. :)
Wouldn't an "update" require looping through each member of each container object?
I was looking for an algorithm that didn't have any linear (or greater) dependancies.
I'm guessing the most efficient would be using a list of containers in each member and container.
#10
Posted 29 April 2006 - 02:52 PM
poita said:
Wouldn't an "update" require looping through each member of each container object?
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.
-
Currently working on: the 3D engine for Tomb Raider.
#11
Posted 30 April 2006 - 11:16 AM
poita said:
Wouldn't an "update" require looping through each member of each container object?
Yet another option is to have a registry of "groups"; whenever you want to kill a person, you inform this registry and it in turn notifies all the containers. If your deletes are relatively rare and you don't mind having an additional dependency, this solution is good, clean, and doesn't add any persistent overhead.
You can see there are many solutions to the problem, each with its own caveats and strengths, but only you know which one is better suited to your specific situation.
#12
Posted 30 April 2006 - 12:30 PM
.oisyn said:
Aren't you doing that anyway sooner or later, like when iterating the container to display the list of persons on the screen?
Good point... :wallbash:
I guess I wasn't thinking straight.
Thanks Jare for your input.
#13
Posted 03 May 2006 - 04:01 AM
Still, a good question. I love these types of discussions.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












