Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int globalgarbage;
- unsigned int anumber = 42;
- void doSomething()
- {
- static bool globalish = true;
- // ...
- }
- include <iostream>
- #include <string>
- using namespace std;
- class test
- {
- public:
- test(const char *name)
- : _name(name)
- {
- cout << _name << " created" << endl;
- }
- ~test()
- {
- cout << _name << " destroyed" << endl;
- }
- string _name;
- };
- test t("global variable");
- void f()
- {
- static test t("static variable");
- test t2("Local variable");
- cout << "Function executed" << endl;
- }
- int main()
- {
- test t("local to main");
- cout << "Program start" << endl;
- f();
- cout << "Program end" << endl;
- return 0;
- }
- global variable created
- local to main created
- Program start
- static variable created
- Local variable created
- Function executed
- Local variable destroyed
- Program end
- local to main destroyed
- static variable destroyed
- global variable destroyed
- int foo(int i)
- {
- static int s = foo(2*i); // recursive call - undefined
- return i+1;
- }
- int foo = init(); // bad if init() throws something
- int main() {
- try {
- ...
- }
- catch(...){
- ...
- }
- }
- int& foo() {
- static int myfoo = init();
- return myfoo;
- }
Add Comment
Please, Sign In to add comment