|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
三个文件Point.h、Point.cpp和main.cpp
Point.h文件内容:
- #ifndef _POINT_
- #define _POINT_
- class Point{
-
- public:
- int x;
- int y;
-
- public:
- Point(int _x=0, int _y=0);
- ~Point();
- };
- #endif // _POINT_
复制代码
Point.cpp文件内容:
- #include "Point.h"
- #include <iostream>
- using namespace std;
- Point::Point(int _x, int _y){
- this->x = _x;
- this->y = _y;
- cout << this << " construction..." << endl;
- }
- Point::~Point(){
- cout << this << " destruction..." << endl;
- }
复制代码
main.cpp文件内容:
- #include "Point.h"
- #include <iostream>
- using namespace std;
- Point setPosition(int x, int y){
- Point p(x, y);
- cout << "the address of point is " << &p << endl;
- return p;
- }
- int main(int argc, char* argv[]){
-
- Point p;
- cout << "the address of point is " << &p << endl;
-
- p = setPosition(2, 4);
- cout << "the address of point is " << &p << endl;
-
- return 0;
-
- }
复制代码
用vs2015的cl.exe编译链接器编译运行,结果如下:
0018FEBC construction...
the address of point is 0018FEBC
0018FE84 construction...
the address of point is 0018FE84
0018FE84 destruction...
0018FEB4 destruction...
the address of point is 0018FEBC
0018FEBC destruction...
其中0018FEB4 destruction...这一句没看懂,有没有大牛解释下,谢谢啦~
按照常规思维,构造和析构应该成对出现,地址为0018FE84的这个类应该是函数setPosition里的局部类对象的地址,函数执行完退出时执行析构函数,输出0018FE84 destruction...,这个可以理解,但是0018FEB4 destruction...这句说明还有一个类对象被析构了?,但是它在哪里构造的呢? |
|