Last active
January 4, 2018 02:12
-
-
Save luchenqun/0256b1eaa44100967ea8eaf2c1406132 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class InnerClassA | |
{ | |
public: | |
InnerClassA() { cout << "in create of InnerClassA" << endl; } | |
}; | |
class InnerClassB | |
{ | |
public: | |
InnerClassB() { cout << "in create of InnerClassB" << endl; } | |
~InnerClassB() { cout << "in destruction of InnerClassB" << endl; } | |
}; | |
class WrapperClassA | |
{ | |
public: | |
WrapperClassA() {} | |
InnerClassA &singleton() | |
{ | |
static InnerClassA innerObjA; //local static object | |
return innerObjA; | |
} | |
}; | |
// class with non-local static object | |
class WrapperClassB | |
{ | |
public: | |
WrapperClassB() {} | |
//static data member declaration | |
static InnerClassB innerObjB; | |
}; | |
//static member definition | |
InnerClassB WrapperClassB::innerObjB; | |
int main(int argc, char *argv[]) | |
{ | |
cout << "main() started." << endl; | |
WrapperClassA objA; | |
objA.singleton(); //只有执行该语句时,innerObjA才被构造出来 | |
cout << "main() terminated." << endl; | |
return 1; | |
} | |
// in create of InnerClassB | |
// main() started. | |
// in create of InnerClassA | |
// main() terminated. | |
// in destruction of InnerClassB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment