1.赋值运算符函数
程序来自何海涛《剑指OFFER》一书
方法1:手动分配内存,调用strcpy
#include <iostream> class CMyString{ public: CMyString(char* pData = NULL); CMyString(const CMyString & str); ~CMyString(void); private: char* m_pData; }; // Reference &: read as "address of" // Dereference *: read as "value pointed by" // 1. Return value type must be of reference & : for consecutive assignment // like: str1 = str2 = str3 // 2. Parameter type must be of const reference & CMyString& CMyString::operator = (const CMyString &str){ if(this == &str ) // this holds the address of str return *this; // return the value at address of str, this value == str // release memory before assign new memory space to avoid memo leak delete [] m_pData; m_pData = NULL; m_pData = new char[strlen(str.m_pData)+1]; strcpy(m_pData, str.m_pData); return *this; }
方法2:不用手动内存分配,借用char指针自己的“=”运算符规避了手动分配内存的烦琐和异常安全风险,移花接木,暗渡陈仓,深得我心!
#include <iostream> using namespace std; class CMyString{ public: CMyString(char* pData = NULL); CMyString(const CMyString & str); ~CMyString(void); CMyString& operator=(const CMyString &str); void Print(); private: char* m_pData; }; CMyString::CMyString(char *pData) { if(pData == NULL) { m_pData = new char[1]; m_pData[0] = '\0'; } else { int length = strlen(pData); m_pData = new char[length + 1]; strcpy(m_pData, pData); } } CMyString::CMyString(const CMyString &str) { int length = strlen(str.m_pData); m_pData = new char[length + 1]; strcpy(m_pData, str.m_pData); } CMyString::~CMyString() { delete[] m_pData; } void CMyString::Print() { printf("%s", m_pData); } CMyString& CMyString::operator=(const CMyString &str){ if(this != &str){ // create temp instance given parameter instance str CMyString strTemp(str); char *pTemp = strTemp.m_pData; strTemp.m_pData = m_pData; m_pData = pTemp; } return *this; } int main(){ char* origData = "original data is 25"; CMyString origStr(origData); cout<<" The original instance origStr contains: "<<endl; origStr.Print(); CMyString newStr; // Test 1: assign origStr to newStr newStr = origStr; cout<<"\n Test 1: After assginment, the new instance newStr contains:"<<endl; newStr.Print(); // Test 2: assing to itself origStr = origStr; cout<<"\n Test 2: After assign to itself, the origStr contains:"<<endl; origStr.Print(); // Test 3: consecutive assingment CMyString str1, str2; str1 = str2 = origStr; cout<<"\n Test 3: After consecutive assignments, the str1 contains:"<<endl; str1.Print(); cout<<"\n Test 3: the str2 contains:"<<endl; str2.Print(); return 0; }
The outputs of above code:
The original instance origStr contains: original data is 25 Test 1: After assginment, the new instance newStr contains: original data is 25 Test 2: After assign to itself, the origStr contains: original data is 25 Test 3: After consecutive assignments, the str1 contains: original data is 25 Test 3: the str2 contains: original data is 25
下载:
git clone https://github.com/zhutiti/Hehaitao.git
参考:
2022年4月05日 09:21
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. https://www.posterprintcenter.com/
2023年7月09日 23:02
Mmm.. estimable to be here in your report or notify, whatever, I repute I should moreover process strong for my have website want I play some salubrious further updated busy in your location. Kissimmee Exterminator
2023年7月19日 17:10
This is exciting, nevertheless it is vital for you to visit this specific url: Orlando Exterminator
2023年7月19日 17:14
Interestingly you write, I will address you'll find exciting and interesting things on similar topics. Oviedo Exterminator
2023年8月12日 21:19
it's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information. Tax Preparation
2023年9月11日 20:40
It is somewhat fantastic, and yet check out the advice at this treat. Managed Service Provider