zoukankan      html  css  js  c++  java
  • C++学习笔记-2-构造函数和析构函数

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

    做了一个实验:

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

    输出为:

    Create Object
    runObject end
    Delete Object
    end

    在void  runObject()中加一个{}

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

    输出为:

    Create Object
    Delete Object
    runObject end
    end

  • 相关阅读:
    一、flink架构模型
    每日看点
    argparse模块用法实例
    Python 牛刀小试
    spark 编程基础
    我想过的100种暴富机会
    hadoop大数据架构
    centOS7 ip 配置
    classNotFound异常的一个原因
    linux上部署java项目
  • 原文地址:https://www.cnblogs.com/learning-c/p/4668960.html
Copyright © 2011-2022 走看看