|
|
发表于 2012-2-14 19:53:27
|
显示全部楼层
因为你定义的是对象 和 main里面变量生命期一样的
return 0时按F11才析构的
如果你定义对象前后加{} 变成块作用域试试
给你贴个代码 这个昨天学得 蛮有用得 分析好了下面这段代码就懂了构造顺序 析构顺序
指向对象 一些问题
----------------------------------------------------------- // 构造析构顺序.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream.h>
- //先成员构造 ---> 自己构造
- //自己析构 ---> 先成员析构
- class B
- {
- public:
- B()
- {
- cout << "B(): " << hex << this << endl;
- }
- ~B()
- {
- cout << "~B(): " << hex << this << endl;
- }
- };
- class C
- {
- public:
- C()
- {
- cout << "C(): " << hex << this << endl;
- }
- ~C()
- {
- cout << "~C(): " << hex << this << endl;
- }
- };
- class A
- {
- public:
- A* theA;
- B theB;
- C theC;
- A()
- {
- cout << "A(): " << hex << this << endl;
- }
- A( const A& obj ):theC(),theB()
- {
- cout << "A() const A& obj: " << hex << this << endl;
- }
- ~A()
- {
- cout << "~A(): " << hex << this << endl;
- }
- };
- A Fun(A obj)
- {
- return obj;
- }
- int main(int argc, char* argv[])
- {
- A obj1;
-
- A* lpobj2 = &Fun(obj1);
-
- A& obj2 = Fun(obj1);
-
- return 0;
- }
复制代码 |
|