When a function, like f(), is fully default-argumented, the invocations without any argument to that function work.
For example, if the function prototype is:
void f(int = 1, int = 1);
then one may call f in this form:
f();
Why is not this the case for default constructors, though they are but functions?
class cls {
public:
int i = 10;
cla(int i = 10): i(i){}
};
int main() {
cls c();
cout << c.i << endl;
}
Defining the default constructor, I expect the statement “cls c();” invokes the default constructor and the program compiles smoothly. However, the compiler returns this error message:
error: request for member ‘i’ in ‘c’, which is of non-class type ‘cls()’
Of course, the code compiles correctly in this form: cls c;
Why do not things go according to my expectations?
Ali Ahmadian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1