构造函数和析构函数的使用

来源:互联网 发布:黄牛抢票软件个人版 编辑:程序博客网 时间:2024/06/16 00:15

#include "iostream"#include "string"class CVector{        std::string *ptr;    public:        //Default constructor   // 默认构造函数        CVector(){            ptr = new std::string;        }        //Constructor with parameters   //带有一个参数的构造函数        CVector(std::string s):ptr(new std::string(s)){}        // Deconstructor        ~CVector(){                    //析构函数            std::cout << content() << std::endl;            std::cout << "release me" << std::endl;            delete ptr;        }        const std::string & content(){return  *ptr;}};int main(){    CVector ca;    CVector cb("abc");}
output:

abc

release me


release me





0 0