I received the following email from a student: > I’m getting an infinite loop on the following code. It keeps going between these two functions whenever I try to assign a RationalNumber to another (and then crashes). I don’t see where the infinite copying is happening.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
When we try to assign
r2 in the following
1 2 3 4
Recall that a typical
swap (x,y) requires a third (local)
variable and the entire operation translates to the following sequence that
requires one clone constructor and two assignments:
1 2 3
std::swap will attempt to use the move
clone constructor and move assignment (to avoid copying overhead). However,
RationalNumber provides neither “move constructor”
nor “move assignment”,
std::swap will fall back
to the copy constructor and copy assignment.
The latter invocation results in an infinite chain of recursive calls.
std::swap call at line 12 attempts to make
a recursive call to the copy assignment at line 9).