浅拷贝与深拷贝的概念是在类的复制/拷贝构造函数中出现的。
private:
xxx *pStr;
......
pStr = m.pStr;//拷贝的是地址
pStr = new xxx[xxx];//先申请内存
对pStr赋值//再拷贝值
浅拷贝
1 #pragma once 2 class Demo 3 { 4 private: 5 int m_icount; 6 int *pStr; 7 public: 8 Demo(int icount);//构造函数 9 Demo(const Demo &d);//拷贝构造函数(浅拷贝) 10 ~Demo();//析构函数 11 void setValue(int icount); 12 void printValue(); 13 void printAddr(); 14 };
1 #include "Demo.h" 2 #include <iostream> 3 using namespace std; 4 5 Demo::Demo(int icount) 6 { 7 m_icount = icount; 8 pStr = new int[m_icount]; 9 for (int i = 0; i < m_icount; i++) 10 { 11 pStr[i] = i; 12 } 13 cout << "\nDemo(int value)构造函数被调用!" << endl; 14 } 15 16 Demo::Demo(const Demo &d) 17 { 18 m_icount = d.m_icount; 19 //浅拷贝 20 pStr = d.pStr;//浅拷贝,赋地址 21 cout << "Demo(const Demo &d)浅拷贝构造函数被调用!" << endl; 22 23 //深拷贝 24 /*pStr = new int[m_icount]; 25 for (int i = 0; i < m_icount; i++) 26 { 27 pStr[i] = d.pStr[i]; 28 } 29 cout << "\nDemo(const Demo &d)深拷贝构造函数被调用!" << endl;*/ 30 } 31 32 Demo::~Demo() 33 { 34 delete[]pStr; 35 pStr = NULL; 36 cout << "\n~Demo()析构函数被调用!" << endl; 37 } 38 39 void Demo::setValue(int icount) 40 { 41 m_icount = icount; 42 cout << "\nsetValue(int icount)函数被调用!" << endl; 43 } 44 45 void Demo::printValue() 46 { 47 for (int i = 0; i < m_icount; i++) 48 { 49 cout << pStr[i] << " "; 50 } 51 cout << "\nDemo::printValue()函数被调用!" << endl; 52 } 53 54 void Demo::printAddr() 55 { 56 cout << pStr << endl; 57 } 58 59 int main() 60 { 61 Demo d1(6); 62 cout << "\nd1 : "; 63 d1.printValue(); 64 65 d1.setValue(5); 66 cout << "\nd1 : "; 67 d1.printValue(); 68 69 Demo d2(d1); 70 cout << "\nd2 :"; 71 d2.printValue(); 72 73 cout << "\nd1 addr : "; 74 d1.printAddr(); 75 cout << "\nd2 addr : "; 76 d2.printAddr(); 77 78 system("pause"); 79 }
结果
深拷贝
1 #include "Demo.h" 2 #include <iostream> 3 using namespace std; 4 5 Demo::Demo(int icount) 6 { 7 m_icount = icount; 8 pStr = new int[m_icount]; 9 for (int i = 0; i < m_icount; i++) 10 { 11 pStr[i] = i; 12 } 13 cout << "\nDemo(int value)构造函数被调用!" << endl; 14 } 15 16 Demo::Demo(const Demo &d) 17 { 18 m_icount = d.m_icount; 19 //浅拷贝 20 /*pStr = d.pStr;//浅拷贝,赋地址 21 cout << "Demo(const Demo &d)浅拷贝构造函数被调用!" << endl;*/ 22 23 //深拷贝 24 pStr = new int[m_icount]; 25 for (int i = 0; i < m_icount; i++) 26 { 27 pStr[i] = d.pStr[i]; 28 } 29 cout << "\nDemo(const Demo &d)深拷贝构造函数被调用!" << endl; 30 } 31 32 Demo::~Demo() 33 { 34 delete[]pStr; 35 pStr = NULL; 36 cout << "\n~Demo()析构函数被调用!" << endl; 37 } 38 39 void Demo::setValue(int icount) 40 { 41 m_icount = icount; 42 cout << "\nsetValue(int icount)函数被调用!" << endl; 43 } 44 45 void Demo::printValue() 46 { 47 for (int i = 0; i < m_icount; i++) 48 { 49 cout << pStr[i] << " "; 50 } 51 cout << "\nDemo::printValue()函数被调用!" << endl; 52 } 53 54 void Demo::printAddr() 55 { 56 cout << pStr << endl; 57 } 58 59 int main() 60 { 61 Demo d1(6); 62 cout << "\nd1 : "; 63 d1.printValue(); 64 65 d1.setValue(5); 66 cout << "\nd1 : "; 67 d1.printValue(); 68 69 Demo d2(d1); 70 cout << "\nd2 :"; 71 d2.printValue(); 72 73 cout << "\nd1 addr : "; 74 d1.printAddr(); 75 cout << "\nd2 addr : "; 76 d2.printAddr(); 77 78 system("pause"); 79 }
结果
原文:https://www.cnblogs.com/yocichen/p/10359542.html