By assigning Ball b = a;
, what it does is just to create a shallow copy of your object. So a and b have different allocated memory. Any change to properties in a (by value) has no effect to b.
The answer is you need to assign using reference:
Ball &b = a;
Just some little extra info, I'd like to explain more about the "shallow" word that I stressed in the above:
class X {
int r;
}
class A {
X *myX;
}
class B {
X *myB;
}
If I set A a; B b =a;
then a.myX.r = 10;
will also affect b.myX.r
This is because object copying is shallow. It doesn't copy what pointer pointing to, but only the pointer itself.