My question is: what constructor is used when instantiating a class c ClassName instance()in C ++?
ClassName instance()
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 is created... using which constructor ? Test instance_2("hello !"); // Ok return 0; }
Thank!
Tricky! You expect compilation to fail because the default constructor is private. However, it compiles and nothing is created. Cause?
Test instance_1();
... it's just a function declaration! (Which returns Testand accepts nothing.)
Test
Test instance_1(); , - instance_1, Test. , 0 , Test instance_1;.
instance_1
Test instance_1;