+ 1

constructor calling

#include<iostream> using namespace std; class A { public: A() { cout<<"zero parameterozed constructor"; } A(int a=0) { cout<<"default parameterized constructor"; } }; int main() { A a(); } why its giving no output?? plz explain

21st Jul 2018, 2:48 PM
Siddharth Jain
Siddharth Jain - avatar
1 Antwort
+ 1
A a(); is a function declaration, it's not constructing an object. https://en.wikipedia.org/wiki/Most_vexing_parse Use A a; to initialize an object without arguments. But with the code you have this still wouldn't compile because the call is ambiguous. A() requires 0 arguments A(int a = 0) requires 0 or 1 argument(s) C++ has 2 options to pick from, but because C++ can't read your mind it just refuses to compile it.
21st Jul 2018, 2:59 PM
Dennis
Dennis - avatar