+ 2
Is constructor's parameter allowed to have a default value? Looks likes No.
I tested it with some codes but I'm still not sure.
1 Respuesta
+ 7
Yes, it is allowed. But it causes ambiguity in cases where you declare a default constructor as well.
Eg - C++ Code:
class A
{
int var;
public :
A() {var = 0;}
A(int v = 0) {var=v;}
};
int main()
{
A obj;
// Error
}
The error occurs because the compiler is unable to determine which constructor you want to call. Is it constructor 1, with no arguments, or constructor 2 with the default 0? Thus there exists an ambiguity.
Only in such cases you can't use default parameters, but for others you can.