-
- #include <fstream>
- using namespace std;
- ofstream out("statdest.txt");
- class Obj
- {
- char c;
- public:
- Obj(char cc) : c(cc) {out<<"Obj::Obj() for "<<c<<endl;}
- ~Obj(){out<<"Obj::~Obj for "<<c<<endl;}
- };
- Obj a('a'); //全局对象
- void f()
- {
- static Obj b('b'); //静态类型
- }
- void g()
- {
- static Obj c('c');
- }
- int main()
- {
- out<<"inside main()"<<endl;
- f();
- g();
- out<<"leaving main()"<<endl;
- return 0;
- }
- /*
- 运行结果:
- Obj::Obj() for a
- inside main()
- Obj::Obj() for b
- Obj::Obj() for c
- leaving main()
- Obj::~Obj for c
- Obj::~Obj for b
- Obj::~Obj for a
- */
C++ 构造函数,析构函数,全局变量
最新推荐文章于 2024-04-29 07:00:00 发布