Parallel Assignment

Hi guys,
I just came across this code fragment. The last statement is the part of interest

1
2
3
4
5
6
void DLinkedList::add(DNode* v, const Elem& e) {	// insert new node before v
	DNode* u = new DNode; u -> value = e;		// create a new node for e
	u -> next = v;					// link u in between v
	u -> prev = v -> prev;				// ...and v -> prev
	v -> prev -> next = v -> prev = u;
}


DLinkedList is a Doubly Linked List. prev is a pointer to the previous node in the list, and next is a pointer to the next node in the list.

I want to confirm that the last statement is a parallel assignment - that is, u is simultaneously assigned to v's prev and v's (old) prev -> next.
Last edited on
Correct. This is because an expression of the form x=y evaluates to the new value of x. E.g. x=y=z is equivalent to y=z; x=y; (when the behavior is x=y=z is defined).
closed account (z05DSL3A)
NewProgrammer wrote:
I want to confirm that the last statement is a parallel assignment - that is, u is simultaneously assigned to v's prev and v's (old) prev -> next.

Incorrect.

(although it might have the desired side effect with some compilers)

See: http://www.cplusplus.com/forum/beginner/43341
Last edited on
Topic archived. No new replies allowed.