c++ - Creating an instance of a class with () -
i have question : constructor used when create instance of class classname instance()
in c++ ?
example:
#include <iostream> using namespace std; class test { private: test() { cout << "aaa" << endl; } public: test(string str) { cout << "string = " << str << endl; } }; int main() { test instance_1(); // instance_1 created... using constructor ? test instance_2("hello !"); // ok return 0; }
thanks !
tricky! expect compilation fail default constructor private. however, compiles , nothing created. reason?
test instance_1();
... function declaration! (which returns test
, takes nothing.)
Comments
Post a Comment