拷贝构造函数深拷贝(10)

网友投稿 213 2022-09-22

拷贝构造函数深拷贝(10)

#ifndef#define#include using namespace std;class A {public: int *a; void show(); A(A &c); A(int b); ~A();};#endif

#include "A.h"A::A(int b) { this->a = new(int); *(this->a) = b; cout << "A::A()" << endl;}A::A(A &b) { //this->a = b.a;//拷贝构造函数,浅拷贝存在内存泄漏 this->a = new (int); *(this->a) = *b.a; cout << "A::A(A &a)" << endl;}A::~A() { delete a; cout << "A::~A()" << endl;}

#include "A.h"#include using namespace std;void fun() { A a1(1); A a2 = a1; cout << "*a2.a = " << *(a2.a) << endl;}int main() { fun();}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:与刘诗诗同场热聊,却与杨幂互不理睬,唐嫣杨幂是怎么闹掰的?
下一篇:嵌入式软件工程师笔试题1
相关文章

 发表评论

暂时没有评论,来抢沙发吧~