深度拷贝的语法(举例类:A)A:(const A& obj) //obj是类A的一个实现以下是实现方法 #头文件 Mytring.h class CMystring { private: char *m_pszBuf; int m_nLenth; public: CMystring(); CMystring(const char*szBuf); ////////////////////////////////////////////////////////////////////////// //深度拷贝构造函数 声明语法A(const A& Temp) ////////////////////////////////////////////////////////////////////////// CMystring(const CMystring& temp); ~CMystring(); ////////////////////////////////////////////////////////////////////////// //注意 无论是参数还是返回值 能用const修饰的尽量使用const修饰 ////////////////////////////////////////////////////////////////////////// const char* Mystrcat(const CMystring *temp); const char* Mystrcat(const CMystring &temp); char* SetData(const char *szBuf); void Init(); //数据成员初始化 void Getlen(); void GetSapce(); void Release(); }; #头文件的实现 Mytring.cpp // Mystring.cpp: implementation of the CMystring class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Mystring.h" #include <iostream.h> #include <string.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// //初始构造 CMystring::CMystring() { Init(); } //有参构造 CMystring::CMystring(const char*szBuf) { Init(); SetData(szBuf); } //深度拷贝构造 ////////////////////////////////////////////////////////////////////////// //语法 A(const A& obj) //以下是实现的方法 //这里是重中之重 //具体思想就是在有指针的时候或者一些有需要的时候 //使用深度拷贝 //利用memcpy将Sou类拷贝给Des类(Des类用this指针表示) 大小事sizeof(类) //然后申请一个堆内存地址 将原指针的数据拷贝到新指针指向的位置 ////////////////////////////////////////////////////////////////////////// CMystring::CMystring(const CMystring& temp) { //全部拷贝过来 memcpy(this, &temp, sizeof(CMystring)); //修改指针拷贝 m_pszBuf = new char[m_nLenth]; ////////////////////////////////////////////////////////////////////////// //这个地方是m_nLenth 千万不要用sizeof(m_nLenth) 因为申请了新地址 然后要 //在新地址拷贝原地址的数据 ////////////////////////////////////////////////////////////////////////// memcpy(m_pszBuf, temp.m_pszBuf, m_nLenth); } CMystring::~CMystring() { Release(); } ////////////////////////////////////////////////////////////////////////// //这个地方之前好像不能用const修饰 【待定】 char* CMystring::SetData(const char *szBuf) { if (!szBuf) { cout << "字符串不能为空!" << endl; return NULL; } Release(); int nLen = strlen(szBuf) + sizeof(char); m_pszBuf = new char[nLen]; strcpy(m_pszBuf, szBuf); m_nLenth = nLen; return m_pszBuf; } ////////////////////////////////////////////////////////////////////////// //字符串连接 ////////////////////////////////////////////////////////////////////////// const char* CMystring::Mystrcat(const CMystring *temp) { strcat(this->m_pszBuf, (*temp).m_pszBuf); return m_pszBuf; } //Mystrcat函数重载 const char* CMystring::Mystrcat(const CMystring &temp) { strcat(this->m_pszBuf, temp.m_pszBuf); return m_pszBuf; } //数据成员初始化 void CMystring::Init() { m_pszBuf = NULL; m_nLenth = 0; } //释放数据成员(指针) void CMystring::Release() { if (!m_pszBuf) { delete[] m_pszBuf; m_pszBuf = NULL; } } void CMystring::Getlen() { cout <<"字符串长度为: " << m_nLenth - 1 << " 空间" << endl; } void CMystring::GetSapce() { cout <<"字符串占 据: " << m_nLenth << " 空间" << endl; } #Main函数 Main.cpp #include "stdafx.h" #include <iostream.h> #include "Mystring.h" #include <string.h> int main(int argc, char* argv[]) { CMystring StrTest1("hello!"); CMystring StrTest2("world!"); CMystring &pStrTest2 = StrTest2; cout << "两个对象字符串连接显示: " << StrTest1.Mystrcat(StrTest2) << endl; CMystring StrTest3("欢迎!"); CMystring StrTest4("来到C++世界!"); CMystring &pStrTest5 = StrTest4; cout << "两个对象字符串连接显示: " << StrTest3.Mystrcat(&pStrTest5) << endl; return 0; }