swapping references

Hi guys, not sure how references work, so I am looking for a little clarification.
For instance take the following code:
1
2
3
4
5
 deque _q1; //actual variables
 deque _q2;

 deque& q1_ref = _q1;// q1 refers _q1;
 deque& q2_ref = _q2;// q2 refers _q2; 

is it possible to swap q1_ref with q2_ref in a way like this:
1
2
3
4
5
{
deque& tmp = q1_ref;
q1_ref = q2_ref;
q2_ref = tmp;
}

am I creating copies of the deques? and what's the difference between q1_ref &= q2_ref and q1_ref = q2_ref?

Thanks for any consideration
- Atari
You can't change what a reference is referencing. Your code will just swap the deques (by copying).

For primitive types q1_ref &= q2_ref is the same as q1_ref = q1_ref & q2_ref. & is the bitwise AND operator.

q1_ref = q2_ref and this is just ordinary assign using operator=.
If what you're asking is whether it's possible to do something in the ellipsis so that:
1
2
3
4
5
6
7
deque& q1_ref = _q1;// q1 refers _q1;
deque& q2_ref = _q2;// q2 refers _q2;

//...

//q1 refers to _q2
//q2 refers to _q1 
then the answer is no.

what's the difference between q1_ref &= q2_ref and q1_ref = q2_ref?
What do you mean?
dunno, thought it was some weird method to pass a reference:)

-Atari
Topic archived. No new replies allowed.