Ah, I see.
It looks like your
Time class is a decendant of the
Object class (as all the objects you add into the list must be).
It is using a horrible trick. Polymorphism works through pointers. So normally you write methods that take pointers. It would be something like:
void DoubleList::addAtHead( Object* toAdd );
But the code you've got wants an actual reference to an Object, not a pointer (yes, language semantics... but there are some guarantees in references not in pointers).
So when you add to the queue with
l.add( *(new Time(Sample)) );
what the compiler says is, "hey, that's supposed to be an
Object -- apply polymorphism and it
is -- sweet":
l.add( *( (Object*)(new Time(Sample)) ) );
Finally, the new object is dereferenced to get something that can be made into a reference.
Here's the rub. What happens to that
Time object that was dynamically allocated on the heap? Is it lost, or would it just be simpler to say:
1 2 3 4 5 6 7 8 9 10 11
|
void DoubleList::addAtHead( Object* toAdd )
{
ListElement* newElement =
new ListElement( toAdd, head, head->next );
CHECK( newElement != 0 );
head->next->prev = newElement;
head->next = newElement;
itemsInContainer++;
}
|
Hope this helps.