1
2
3
4
5
6
7
8
9
10
11
12
| void PrintValues(int nValue1, int nValue2=10){ using namespace std; cout << "1st value: " << nValue1 << endl; cout << "2nd value: " << nValue2 << endl;}int main(){ PrintValues(1); // nValue2 will use default parameter of 10 PrintValues(3, 4); // override default value for nValue2} |
1
2
3
| void OpenLogFile(char *strFilename="default.log");int RollDie(int nSides=6);void PrintString(char *strValue, Color eColor=COLOR_BLACK); // Color is an enum |
1
2
3
4
5
| void PrintValues(int nValue1=10, int nValue2=20, int nValue3=30){ using namespace std; cout << "Values: " << nValue1 << " " << nValue2 << " " << nValue3 << endl;} |
1
2
3
4
| PrintValues(1, 2, 3);PrintValues(1, 2);PrintValues(1);PrintValues(); |
PrintValues(,,3). This has two major consequences:
1
| void PrintValue(int nValue1=10, int nValue2); // not allowed |
1
2
| void Print(char *strString);void Print(char ch=' '); |
Print(), it would resolve to Print(' '), which would print a space.
1
2
| void PrintValues(int nValue);void PrintValues(int nValue1, int nValue2=20); |
PrintValues(10), the compiler would not be able to disambiguate whether the user wanted PrintValues(int) or PrintValues(int, 20) with the default value.

0 comments: