有关析构的顺序

来源:互联网 发布:微商城源码 编辑:程序博客网 时间:2024/05/01 16:04
#include <iostream>using namespace std;class num{public:num(){n = new int;cout << "不带参数的构造函数调用\n";}num ( int i ){n = new int;*n = i;cout << "带一个参数的构造函数调用\n";}~num(){delete n;n = 0;cout << "析构函数调用\n";}int get() const{return *n;}num equal ( const num&r ){*n = r.get();return *this;//返回的是 two的副本}private:int *n;};int main(){num *one = new num ( 3 );num two;two.equal ( *one );//第一次析构,析构的是副本//  cout<<"one:"<<one->get()<<endl;delete one;第二次析构return 0;//结束,第三次析构two,但是two所指向内容地址早被第二次析构,所以出错}

原创粉丝点击