




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、student.h#ifndef STUDENT_H#define STUDENT_Hclass Studentpublic:Student(char* pName="no name",int ssID=0);Student(Student& s);/拷貝構造函數(shù)Student();protected:char name40;int id;#endifstudent.cpp#include <iostream>#include <string>#include "student.h"using namespace std;
2、Student:Student(char* pName,int ssID)strcpy(name,pName);cout<<"constructing new student"<<pName<<endl;Student:Student(Student& s) /拷貝構造函數(shù),必須用引用或指針,不能直接用student(student s) /因為不能像基本數(shù)據(jù)類型那樣直接傳遞對象的值。 cout<<"constructing copy of"<<<<endl;st
3、rcpy(name,"copy of");strcat(name,);id=s.id;Student:Student()cout<<"destructing"<<name<<endl;/* *test.cpp *堆與拷貝構造函數(shù)的測試 *2011/10/7 *劉珅珅*/#include <iostream>#include <string>#include "student.h"using namespace std;void fn(Student s);int
4、main(int argc,char* argv)Student randy("Randy",1234);cout<<"calling fn()n"fn(randy);/要調(diào)用拷貝構造函數(shù)cout<<"returned from fn()n" Student jenny=randy;/調(diào)用拷貝構造函數(shù)return 0;void fn(Student s)cout<<"In function fn()n"輸出的結果:有拷貝構造函數(shù):constructing new studentR
5、andycalling fn()constructing copy of RandyIn functiong fn()destructingcopy ofRandy/fn()函數(shù)結束,發(fā)生析構,析構的是fn中s對象returned from fn()constructing copy of Randy/拷貝構造jenny對象destructingcopy ofRandy/析構jenny對象destructingRandy/析構了randy對象沒有拷貝構造函數(shù):constructing new studentRandycalling fn()In functiong fn()/當沒有自定義拷貝構
6、造函數(shù)時,C+會調(diào)用默認的拷貝構造函數(shù)/但類擁有資源時,采用默認的拷貝構造函數(shù)容易出問題destructingRandy/fn()函數(shù),發(fā)生析構,析構對象sreturned from fn()destructingRandy/析構對象jennydestructingRandy用一個類對象區(qū)構造一個新的對象時,如果沒有自定義的拷貝構造函數(shù),C+會調(diào)用默認的拷貝構造函數(shù),但如果構造函數(shù)中有資源(堆內(nèi)存)時,會出問題,因為默認的拷貝構造函數(shù)只會簡單的拷貝資源,而不會分配資源,這時會出現(xiàn):兩個對象都擁有同一個資源的局面,在析構時會產(chǎn)生錯誤。這時需要自定義拷貝構造函數(shù)做深拷貝。person.h#ifnd
7、ef PERSON_H#define PERSON_Hclass Personpublic:Person(char* pN);Person(Person& p);/拷貝構造函數(shù)Person();protected:char* pName;#endifperson.cpp#include <iostream>#include <string>#include "person.h"using namespace std;Person:Person(char* pN)cout<<"constructing "<
8、<pN<<endl;pName=new charstrlen(pN)+1;if(pName!=0)strcpy(pName,pN);Person:Person(Person& p)/* *這個拷貝構造函數(shù)是必須的,原因是析構函數(shù)中使用deltete將堆空間返回給系統(tǒng) *如果沒有拷貝構造函數(shù),而使用默認的拷貝構造函數(shù),那么后面的程序中p1,p2將擁有完全相同的堆空間,在析構時,析構了p2后,堆空間已經(jīng)返回給系統(tǒng),再析構p1時,執(zhí)行delete pName;就會出錯*/cout<<"Copying"<<p.pName<&l
9、t;"into its own blockn"pName=new charstrlen(p.pName)+1;if(pName!=0)strcpy(pName,p.pName);Person:Person()cout<<"destructing"<<pName<<endl;pName0='0'delete pName; test.cpp/* *test.cpp *堆與拷貝構造函數(shù)的測試 *2011/10/7 *劉珅珅*/#include <iostream>#include <string>#include "person.h"using namespace std;int main(int argc,char* arg
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 餐飲連鎖加盟與區(qū)域代理合作協(xié)議范本
- 餐飲門面租賃合同租賃終止條件與違約責任解析
- 員工培訓案例
- 茶園承包與茶葉品牌保護與維權合作協(xié)議
- 生態(tài)工業(yè)園區(qū)廠房土地抵押借款合同
- 餐飲連鎖品牌加盟加盟商權益保障合同
- 智能家居系統(tǒng)承包安裝服務合同范本
- 拆除工程安全責任書:建筑拆除安全合同
- 名醫(yī)診療經(jīng)驗傳承師承合同
- 師生夏季安全教育
- 2025年湖南省中考英語試卷真題(含答案)
- 護理急診急救培訓課件
- 2025年衛(wèi)生系統(tǒng)招聘考試(公共基礎知識)新版真題卷(附詳細解析)
- 2024年司法局司法輔助崗招聘考試筆試試題(含答案)
- 重慶市普通高中2025屆高一下化學期末學業(yè)質(zhì)量監(jiān)測試題含解析
- 2025年人力資源管理師考試試卷及答案
- 北方華創(chuàng)招聘筆試題庫2025
- 2025鄭州航空工業(yè)管理學院輔導員考試試題及答案
- 浙江省嘉興市2023-2024學年高一下學期6月期末考試英語試題(含答案)
- 多模態(tài)數(shù)據(jù)融合的智能告警機制-洞察闡釋
- 2024年廣東省臺山市事業(yè)單位公開招聘教師崗考試題帶答案分析
評論
0/150
提交評論