我想使用xor 算法交换两个局部变量,但是它不允许我更改变量的地址。如果我创建指向变量的指针,它将允许它,但是否可以在不使用指针的情况下做到这一点?
我正在尝试这样做
Point a = Point();
Point b = Point(1,1);
&a ^= &b;
&b ^= &a;
&a ^= &b;
这是我最接近我想做的事情
Point a = Point();
Point b = Point(1,1);
Point *a_ptr = &a;
Point *b_ptr = &b;
a_ptr = (Point *)(((unsigned long)a_ptr) ^ (unsigned long)b_ptr);
b_ptr = (Point *)(((unsigned long)a_ptr) ^ (unsigned long)b_ptr);
b_ptr = (Point *)(((unsigned long)a_ptr) ^ (unsigned long)b_ptr);
// The pointers are switched, but this will not work
&a = a_ptr;