首页 > 编程语言 > 详细

C++学习笔记-2-构造函数和析构函数

时间:2015-07-23 00:27:38      阅读:237      评论:0      收藏:0      [点我收藏+]

问题2. 什么时候执行构造函数和析构函数  22:59:40 2015-07-22

做了一个实验:

#include <iostream>
class Object{
public:
    Object(){
        printf("Create Object\n");
    };
    ~Object(){
        printf("Delete Object\n");
    }
};
void runObject(){
    Object obj;
    printf("runObject end\n");
}
int main(){
//    Object *obj=new Object();
//    delete obj;
    runObject();
    printf("end\n");
    return 0;
}

输出为:

Create Object
runObject end
Delete Object
end

在void  runObject()中加一个{}

#include <iostream>
class Object{
public:
    Object(){
        printf("Create Object\n");
    };
    ~Object(){
        printf("Delete Object\n");
    }
};
void runObject(){
    {
        Object obj;
    }
    printf("runObject end\n");
}
int main(){
//    Object *obj=new Object();
//    delete obj;
    runObject();
    printf("end\n");
    return 0;
}

输出为:

Create Object
Delete Object
runObject end
end

 

C++学习笔记-2-构造函数和析构函数

原文:http://www.cnblogs.com/learning-c/p/4668960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!