The default copy constructor can complete a simple copy of the object's data member values
When the data resource for an object is a heap indicated by a pointer, the default copy constructor is only used as a pointer value to copy
#define _crt_secure_no_warnings#include <iostream>using namespace STD;classname{ Public: Name (Const Char*MYP) {M_len =strlen(MYP); M_p = (Char*)malloc(M_len +1);// strcpy(m_p, MYP); }//name obj2 = obj1; //Solution: Manually write copy constructors using deep copyName (Constname& obj1) {m_len = Obj1.m_len; M_p = (Char*)malloc(M_len +1);strcpy(m_p, obj1.m_p); } ~name () {if(m_p! = NULL) { Free(m_p); m_p = NULL; M_len =0; } }protected:Private:Char*m_p;intM_len; };voidObjplaymain () {Name obj1 ("ABCDEFG");//name obj2 = obj1; A shallow copy of the default copy constructor provided by the C + + compilerName Obj3 ("Obj3"); Obj3 = obj1;the//C + + compiler provides a shallow copy of the equal sign operation}voidMain91 () {objplaymain ();cout<<"Hello ..."<<endl; System"Pause");return;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + deep copy and shallow copy