+ 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.

20th Dec 2017, 5:48 AM
Bo Yang
Bo Yang - avatar
1 Réponse
+ 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.
20th Dec 2017, 5:55 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar