C++ rules for args - reasons?

So, in the Mad Lib chapter of Beginning C++ Through Game Programming (by Michael Dawson), it's stated two things:

1. "Once you specify a default in a list of parameters, you must specify default arguments for all remaining parameters."

1
2
3
4
5
// Correct:
void setDisplay (int height, int width, int depth = 32, bool fullScreen = true);

// Illegal:
void setDisplay (int height, int width, int depth = 32, bool fullScreen);


2. "When you are calling a function with default arguments, once you omit an argument, you must omit arguments for all remaining parameters."

1
2
3
4
5
6
7
8
// Given...
void setDisplay(int height, int width, int depth = 32 bool fullScreen = true);

// Correct:
setDisplay (1680, 1050);

// Illegal:
setDisplay (1680, 1050, false);


Question: Could someone please clarify/confirm the technical reason(s) why these rules exist?

Thanks in advance.
Last edited on
1
2
// Illegal:
setDisplay (1680, 1050, false);

It's not "illegal" but it will not do what you think.

The third parameter is int depth but you passed in false as the third argument. What will happen is that depth will be assigned the value false, which becomes 0 when converted to an int. fullScreen will get the value true, because that is the default argument for fullScreen.
There is no technical reasons why second example in your (1) would be illegal. But it will make default argument useless and in all cases such declaration results from programmer mistake, so language authors decided to make such declaration illegal to help the programmer.
Allright, then. Thanks guys. :)
Topic archived. No new replies allowed.