C++ Default values in functions? -
how default values in functions work? question pertains example:
int func(int number, std::string name = "none", int anothernumber); .. int func(int number, std::string name, int anothernumber){ ... } func(1, 2); ^error, name null yet we've defined default value?
the compiler gives error complaining argument null , should not be. yet i've defined default value it.
why this?
if default parameter @ position k
supplied, parameters in positions k+1
end must supplied well. c++ allows omit parameters in end positions, because otherwise has no way of matching parameter expressions formal parameters.
consider example:
int func(int a, int b=2, int c, int d=4); ... foo(10, 20, 30);
this call ambiguous, because supplies 3 parameters out of four. if declaration above allowed, c++ have choice of calling
func(10, 20, 30, 4);
or
func(10, 2, 30, 40);
with defaulted parameters @ end , rule parameters matched position, there no such ambiguity:
int func(int a, int b, int c=2, int d=4); ... foo(10, 20, 30); // means foo(10, 20, 30, 4);
Comments
Post a Comment