c++ - No error for not initializing reference variable of class -
i newbie , have basic doubt relationship between object creation , constructors.
program- 1
#include<iostream> using namespace std; class xxx{ private: int x; public: xxx(){cout<<"constructer called"<<endl;} }; int main(int argc, char *argv[]) { xxx x1; //constructor called return 0; } output- constructor called
program- 2
#include<iostream> using namespace std; class xxx{ private: int x; public: xxx(){cout<<"constructer called"<<endl;} }; int main(int argc, char *argv[]) { xxx x1(); //constructor xxx() not called. return 0; } output- blank information helpfule
xxx x1; creates object of class xxx, therefore, calls default constructor of class xxx.
xxx x1(); declares function returns object of class xxx , function name x1, takes no parameter. not instantiation of class xxx, therefore, there no constructor being called.
Comments
Post a Comment