Re: Design question - linked list or std::list
On Mar 15, 1:25 pm, Angus <anguscom...@gmail.com> wrote:
I am modelling a telephone system and I have created a call
and a party class. A call can have one to many parties. For
example in a conference call there can be 3 or more parties.
In a call transfer there can be 3 parties and one of the
parties disconnects to transfer the call.
I need to be able to traverse the list of parties on each
call. for example if each party to the call is disconnected I
can remove the call object.
I create the call object as a pointer and so I delete when the
each party to the call has disconnected. The call object has
a std::list of the party objects. Currently, the list is of
party objects, not pointers to party objects.
I'm having trouble with your vocabulary. You don't "create
something as a pointer", you create an object (which probably
isn't a pointer). If your system is anything like the telephone
systems I've worked on, then Party and Call have identity (and
explicit lifetimes), and can't be copied, so all of your
containers would just contain pointers.
The simplest solution would probably be to use std::set for the
parties of a Call, e.g.:
class Call
{
public:
explicit Call( Party* initializingParty )
{
connect( initializingParty ) ;
}
void connect( Party* addedParty )
{
myConnectedParties.insert( addedParty ) ;
}
void disconnect( Party* removedParty )
{
myConnectedParties.erase( removedParty ) ;
if ( myConnectedParties.empty() ) {
delete this ;
}
}
private:
std::set< Party* > myConnectedParty ;
} ;
Of course, you'll need additional logic for other aspects of the
call.
I was wondering if it would be better to change the design so
that the std::list is of pointers to party. Or even to have
the call object create party* 's and the party object to be a
linked list - ie a means to SetNextParty(party* next).
I can't think of why you'd use a list here. (Given the
typically small size of the set, using std::vector would
probably be more efficient, but it requires more code.) And I
can't quite imagine a case where Party would be copiable, and
could be used in a standard container.
But if I implement as a linked list I have the hassle of
having to delete each party. I would do this when the call*
was being deleted.
You only delete a Party when it becomes unknown to the system.
Not when it disconnects from a call.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34