From: hartman@ulogic.UUCP (Richard M. Hartman)
Newsgroups: comp.lang.c++
Subject: Re: Indirect container classes
Date: 22 Dec 92 22:52:27 GMT
Organization: negligable

In article <1992Dec21.100339.14138@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
>In article <1992Dec20.232025.28984@county.oz.au> porum@county.oz.au (Peter Orum) writes:
>>Could someone advise on what is the best method of
>>having a collection of pointers to large objects. 
>>The
>>large objects may appear on more than one list, but I
>>only want one instance of each object  to be created.
>>
>>	a)	a list of pointers, eg List<String *>
>>	
>>	b)	a list class that assumes its  data are pointers
>>		eg Indirect_List<String *>
>>		
>>	c)  use List<String>, but class String is implemented using
>>		a pointer to a reference-counted String_rep.
>>		
>>To  me,  method  a)  is  the easiest to implement but you lose
>>the  ability  to  test  for  list  membership  by  content, as
>>comparison is on absolute memory addresses.
>>
>>Method  b)  has  the  disadvantage  of (slightly) complicating
>>the   List   class, requiring  overloading  of  the  comparison
>>operators, search & copy functions.
>>
>>Method  c)  complicates the class definition, but means in the
>>client  code  I  don't have to decide on which type of List to
>>use, nor whether to use pointers or objects.
>
>	c) looks most worth perusing to me. Without reference
>counting (a or b), how do you plan to delete the large objects
>when they are no longer required? (Or rather, how will
>you know when they are no longer required?)

In my case I know because I add the object to all appropriate
lists and remove it from all appropriate lists at the same
time.

e.g. message comes in: register Student James Cagney, New York, Desk 6

I create the student object for James, and add it to

	a) the master student roster (sorted alphabetically)
	b) the student roster for New York (indexed by Desk number
		and also alphabetically)
	c) for each list, it is also added to a hashing dictionary
		so I can get straight to that student object from the
		name (and can then increment from desk 6 to desk 7, or
		get the next name alphabetically at New York, or from
		the master roster)
	d) POTENTIALLY added to a "wants to ask a question" queue

The point is, reference counting is not (in this case at least)
neccessary, even though the object is used multiple times.  When
it comes time to log Jimmy out, I *do* have to check to see if
he has his hand raised, but other than that he comes out of all
lists at the same time.

I do not have templates available.  I am using NIH-derived list
classes for queues, priority queues, sorted lists etc.  The
Student Object is derived from the good ol' NIH object class,
with links being dynamically allocated for each list I wish
to put him on.  I really, really dislike the idea of having
to specially derive an object from a LinkObject class in order
to put it on a list, a Link points to the next Link, and to
it's "contents" which may be any Object-derived class.

This said, I don't know if it answers the original question,
but it does answer the "how do you know when to delete the
object if you don't use reference counters" question for at
least one application.

		-Richard Hartman
		hartman@uLogic.COM

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"Ideas are not responsible for the people who believe them."

Newsgroups: comp.lang.c++