1) operator+() and operator+=() are different operator, so they are
supposed to provide different result.
2) In this code, when you call printBinary() the second time, the value of the local
prefix is unaffected by the previous call:
1 2
|
printBinary(dec-1,prefix += "0");
printBinary(dec-1,prefix += "1");
|
3) Instead here, at the second call, the local
prefix has already been modified by the previous invocation (because +=
assigns a new value):
1 2
|
printBinary(dec-1,prefix += "0");
printBinary(dec-1,prefix += "1");
|
4) To understand that behaviour, you should keep in mind those are recursive functions.
The second call does not start until the entire chain of ‘first’ calls is ended.
At that point, in the two versions of this function,
prefix holds a different value.
Ok, I really can’t explain it in English :-( but if you try to ‘cout’ it, it’s self explanatory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <string>
void printBinary_1(int dec, std::string prefix = "");
void printBinary_2(int dec, std::string prefix = "");
int main()
{
printBinary_1(3);
std::cout << "- - -\n";
printBinary_2(3);
std::cout << '\n';
}
void printBinary_1(int dec, std::string prefix)
{
if (dec == 0){
std::cout << prefix << '\n';
} else {
std::cout << "_1: prefix: " << prefix
<< "; passing prefix + \"0\".\n";
printBinary_1(dec-1, prefix + "0");
std::cout << "_1: prefix: " << prefix
<< "; passing prefix + \"1\".\n";
printBinary_1(dec-1, prefix + "1");
}
}
void printBinary_2(int dec, std::string prefix)
{
if (dec == 0){
std::cout << prefix << '\n';
} else {
std::cout << "_2: prefix: " << prefix
<< "; passing prefix += \"0\".\n";
printBinary_2(dec-1, prefix += "0");
std::cout << "_2: prefix: " << prefix
<< "; passing prefix += \"1\".\n";
// prefix here has already been modified:
printBinary_2(dec-1, prefix += "1");
}
}
|
Output:
_1: prefix: ; passing prefix + "0".
_1: prefix: 0; passing prefix + "0".
_1: prefix: 00; passing prefix + "0".
000
_1: prefix: 00; passing prefix + "1".
001
_1: prefix: 0; passing prefix + "1".
_1: prefix: 01; passing prefix + "0".
010
_1: prefix: 01; passing prefix + "1".
011
_1: prefix: ; passing prefix + "1".
_1: prefix: 1; passing prefix + "0".
_1: prefix: 10; passing prefix + "0".
100
_1: prefix: 10; passing prefix + "1".
101
_1: prefix: 1; passing prefix + "1".
_1: prefix: 11; passing prefix + "0".
110
_1: prefix: 11; passing prefix + "1".
111
- - -
_2: prefix: ; passing prefix += "0".
_2: prefix: 0; passing prefix += "0".
_2: prefix: 00; passing prefix += "0".
000
_2: prefix: 000; passing prefix += "1".
0001
_2: prefix: 00; passing prefix += "1".
_2: prefix: 001; passing prefix += "0".
0010
_2: prefix: 0010; passing prefix += "1".
00101
_2: prefix: 0; passing prefix += "1".
_2: prefix: 01; passing prefix += "0".
_2: prefix: 010; passing prefix += "0".
0100
_2: prefix: 0100; passing prefix += "1".
01001
_2: prefix: 010; passing prefix += "1".
_2: prefix: 0101; passing prefix += "0".
01010
_2: prefix: 01010; passing prefix += "1".
010101 |