<三>對象的淺拷貝和深拷貝問題
先看示例代碼
點擊查看代碼
#include <iostream>
#include<cstring>
using namespace std;
class Student{
public:
Student(int _age , const char * _name)
{
this->age=_age;
int len=strlen(_name)+1;
char *tep=new char[len];
this->pName=tep;
strcpy(this->pName,_name);
}
~Student(){
delete[]this->pName;
this->pName=nullptr;
}
void showStudent(){
cout<<this->pName<<" "<<this->age<<endl;
}
private:
int age;
char *pName;
};
int main(){
Student s1(20,"zhangsan");
s1.showStudent();
Student s2=s1;
s2.showStudent();
return 1;
}
上面示例代碼中,對象的默認拷貝方式是內(nèi)存數(shù)據(jù)拷貝,如果對象占用了外部資源,那么就會出現(xiàn)問題了,這里的外部資源
就是在堆上申請的空間存放名字用,
s1,s2兩個對象中的名字指針都是指向了同一塊堆內(nèi)存區(qū)域,在結(jié)束main函數(shù)的是,s2先析構(gòu),s2會析構(gòu)調(diào)堆上的內(nèi)存空間,
s1再析構(gòu),由于s1對象的pName 指針和s2對象的pName指針指向的是同一塊堆內(nèi)存區(qū)域,但是該區(qū)域內(nèi)存已經(jīng)被釋放了,所以遇到了問題
鑒于上面的問題,我們引出了 淺拷貝,深拷貝的問題和解決方法.
Student s2=s1; 會調(diào)用拷貝構(gòu)造函數(shù)(該拷貝構(gòu)造函數(shù)可以是系統(tǒng)自動生成的,或者你自己定義一個拷貝構(gòu)造函數(shù))
系統(tǒng)自動生成的拷貝構(gòu)造函數(shù)做的是內(nèi)存數(shù)據(jù)拷貝,所以就出現(xiàn)了上面的問題.因此我們需要定義屬于自己的拷貝構(gòu)造函數(shù)
改造代碼如下
點擊查看代碼
class Student2{
public:
Student2(int _age , const char * _name)
{
this->age=_age;
int len=strlen(_name)+1;
char *tep=new char[len];
this->pName=tep;
strcpy(this->pName,_name);
cout<<"執(zhí)行構(gòu)造函數(shù)"<<endl;
}
//自定義拷貝構(gòu)造函數(shù)
Student2(const Student2 &_stu2){
this->age=_stu2.age;
int len =strlen(_stu2.getPName())+1;
char *newPName =new char[len];
this->pName=newPName;
strcpy(this->pName,_stu2.getPName());
cout<<"執(zhí)行拷貝構(gòu)造函數(shù)"<<endl;
}
~Student2(){
if(this->pName!=nullptr){
delete[]this->pName;
this->pName=nullptr;
}
}
void showStudent(){
int *p=(int *)this->pName;
cout<<this->pName<<" "<<this->age<<" pName Heap Address ="<<p<<endl;
}
const char * getPName() const{
return this->pName;
}
private:
int age;
char *pName;
};
int main(){
Student2 s1(20,"zhangsan");
s1.showStudent();
Student2 s2=s1;
s2.showStudent();
return 1;
}
上面的代碼還存在一個問題如下,如果在main函數(shù)中是下面的代碼
Student2 s1(20,"zhangsan");
Student2 s2(30,"lisi");
s2=s1;
在main函數(shù)結(jié)束的是同樣會遇到問題,這個時候就引出 賦值函數(shù),我們需要定義自己的賦值函數(shù)
在 s2=s1這一段代碼其實是這樣調(diào)用的
s2.operator=(s1)
在你不定義自己的賦值函數(shù)的時候,系統(tǒng)會幫我們生成一個賦值函數(shù),該賦值函數(shù)的賦值方式就是內(nèi)存的數(shù)值拷貝,這種方式
在Student對象,會有問題,所以我們需要向自定義拷貝構(gòu)造一樣,定義一個屬于自己的賦值函數(shù)
代碼如下
點擊查看代碼
class Student3{
public:
Student3(int _age , const char * _name)
{
this->age=_age;
int len=strlen(_name)+1;
char *tep=new char[len];
this->pName=tep;
strcpy(this->pName,_name);
cout<<"執(zhí)行構(gòu)造函數(shù)"<<endl;
}
//自定義拷貝構(gòu)造函數(shù)
Student3(const Student3 & _stu){
this->age=_stu.age;
int len =strlen(_stu.getPName())+1;
char *newPName =new char[len];
this->pName=newPName;
strcpy(this->pName,_stu.getPName());
cout<<"執(zhí)行拷貝構(gòu)造函數(shù)"<<endl;
}
//自定義賦值函數(shù)
Student3 & operator= (const Student3 & _stu){
//防止自賦值
if(this==&_stu){return *this;}
//注意: 需要先釋放當(dāng)前的堆內(nèi)存空間??!
delete []this->pName;
this->age=_stu.age;
int len =strlen(_stu.getPName())+1;
char *newPName =new char[len];
this->pName=newPName;
strcpy(this->pName,_stu.getPName());
cout<<"執(zhí)行賦值函數(shù)"<<endl;
return *this;
}
~Student3(){
if(this->pName!=nullptr){
delete[]this->pName;
this->pName=nullptr;
}
}
void showStudent(){
int *p=(int *)this->pName;
cout<<this->pName<<" "<<this->age<<" pName Heap Address ="<<p<<endl;
}
const char * getPName() const{
return this->pName;
}
private:
int age;
char *pName;
};
int main(){
Student3 s3(20,"zhangsan");
s3.showStudent();
Student3 s4(30,"lisi");
s4=s3;
s4.showStudent();
return 1;
}
運用深拷貝淺拷貝實現(xiàn)String 代碼如下
點擊查看代碼
#include <iostream>
#include <cstring>
using namespace std;
class MyString{
public:
//構(gòu)造函數(shù)
MyString(const char * src){
//創(chuàng)建空串
if(src==nullptr){
this->pchar=new char[1];
this->pchar[0]='\0';
}
else{
int len =strlen(src)+1;
this->pchar=new char[len];
strcpy(this->pchar,src);
}
cout<<"執(zhí)行構(gòu)造函數(shù), 堆內(nèi)存空間地址"<<(int *)this->pchar <<endl;
}
//拷貝構(gòu)造
MyString(const MyString & myString){
//重新分配內(nèi)存空間
int len =myString.stringLen()+1;
this->pchar=new char[len];
strcpy(this->pchar,myString.pchar);
cout<<"執(zhí)行拷貝構(gòu)造函數(shù), 新創(chuàng)建堆內(nèi)存空間地址"<<(int *)this->pchar <<endl;
}
//賦值函數(shù)
MyString & operator=(const MyString & myString){
//防止自賦值
if(this==&myString){return *this;}
//釋放現(xiàn)有的堆內(nèi)存空間
delete[]this->pchar;
this->pchar=nullptr;
int len =myString.stringLen()+1;
this->pchar=new char[len];
strcpy(this->pchar,myString.pchar);
cout<<"執(zhí)行賦值函數(shù), 新創(chuàng)建堆內(nèi)存空間地址"<<(int *)this->pchar <<endl;
return *this;
}
int stringLen() const {
return strlen(this->pchar);
}
//析構(gòu)
~MyString(){
delete [] this->pchar;
this->pchar=nullptr;
}
void printChar() const{
cout<<this->pchar<<endl;
}
private:
char *pchar;
};
int main(){
MyString s1("abcd");
s1.printChar();
MyString s2=s1;//執(zhí)行拷貝構(gòu)造
s2.printChar();
MyString s3("1234");
s3=s1;//執(zhí)行賦值函數(shù)
s3.printChar();
return 1;
}
上一篇:&lt;二&gt;掌握構(gòu)造函數(shù)和析構(gòu)函數(shù)
欄 目:C++
下一篇:&lt;四&gt;構(gòu)造函數(shù)初始化列表
本文標(biāo)題:&lt;三&gt;對象的淺拷貝和深拷貝問題
本文地址:http://mengdiqiu.com.cn/a1/c++/17092.html


閱讀排行
本欄相關(guān)
- 04-02qt用法小結(jié)2021_11_19
- 04-02Visual Studio C++ 默認 UTF-8 編碼及 *.edi
- 04-02&lt;五&gt;關(guān)于類的各類成
- 04-02【C++】extern &quot;C&quot;詳
- 04-02&lt;六&gt;指向類成員的指
- 04-02&lt;三&gt;對象的淺拷貝和
- 04-02&lt;四&gt;構(gòu)造函數(shù)初始化
- 04-02[C++]
- 04-02&lt;二&gt;掌握構(gòu)造函數(shù)和
- 04-02SLAM十四講CH8代碼
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)