Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
ch14_3.cpp
Package: C.zip [view]
Upload User: gzy2011
Upload Date: 2021-02-09
Package Size: 20k
Code Size: 1k
Category:
Compress-Decompress algrithms
Development Platform:
Visual C++
- //ch14_3.cpp
- //关于浅拷贝和深拷贝:p320-p322
- #include"iostream.h"
- #include<string.h>
- class Person
- {
- public:
- Person(char* pN){
- cout<<"constructing"<<pN<<endl;
- pName=new char[strlen(pN)+1];
- if(pName!=0){
- strcpy(pName,pN);
- }
- }
- Person(Person& p){ //自定义拷贝构造函数,实现深拷贝:不但复制了
- //对象空间,而且复制了资源(堆内存空间)
- cout<<"copying"<<p.pName<<"into its own block.n";
- pName=new char[strlen(p.pName)+1];
- if(pName!=0){
- strcpy(pName,p.pName);
- }
- }
- ~Person()
- {
- cout<<"destructing"<<pName<<endl;
- pName[0]='';
- delete pName;
- }
- protected:
- char* pName;
- };
- void main(void)
- {
- Person p1("Randy");
- Person p2=p1; //即Person p2(p1);
- //调用自定义拷贝复制构造函数,为p2另分配资源(而不是像浅拷贝那样共享p1
- //资源)。这样,程序结束时先后析构p2和p1,析构函数才不会出错。
- }