class Base { public: Base() { cout << "cons" << endl; } ~Base() { cout << "de" << endl; } static Base* Default() { static Base b; return &b; } }; int main(int argc, char** argv) { for (int i = 0; i < 3; i++) { Base::Default(); } }
只构造析构一次!
class Base { public: Base() { cout << "cons" << endl; } ~Base() { cout << "de" << endl; } static Base* Default() { static Base b; return &b; } }; int main(int argc, char** argv) { for (int i = 0; i < 3; i++) { Base::Default(); } }
只构造析构一次!