剑指Offer 面试题1:赋值运算函数符
2020-06-29 04:57
981 查看
面试题1:赋值运算符函数
题目:
如下类型CMyString的声明,请为该类型添加赋值运算符操作
考虑要点:
- 返回*this 这样才可以连续赋值 s1=s2=s3
- 传入参数类型申明为常量引用,可以避免值传递再次调用拷贝构造函数
- 在分配新内存之前是否释放实例自身的内存,如果你没有将之前的内存归还而分配了新的内存,那么之前的内存将会泄漏
- 当赋值操作是实例自身的时候直接返回*this,避免释放自身内存后导致严重问题
#pragma warning(disable:4996) #include "stdafx.h" #include <cstring> #include <cstdio> using namespace std; //=================================思路============================================ //考虑要点:1.返回*this 这样才可以连续赋值 s1=s2=s3 // 2.传入参数类型申明为常量引用,可以避免值传递再次调用拷贝构造函数 // 3.在分配新内存之前是否释放实例自身的内存,如果你没有将之前的内存归还而分配了新的内存,那么之前的内存将会泄漏 // 4.当赋值操作是实例自身的时候直接返回*this,避免释放自身内存后导致严重问题 //=============================涉及知识盲点&注意点======================================== // class CMyString{ public: CMyString(char *pData=nullptr); CMyString(const CMyString&str);//拷贝构造函数参数用常量引用,避免循环调用拷贝构造函数 ~CMyString(); CMyString& operator=(const CMyString&str);//常量引用,避免调用拷贝构造,提高效率 void Print(); private: char *m_pData; }; CMyString& CMyString::operator=(const CMyString&str) { if (this!=&str)//先创建一个临时实例,两者互换指针地址,再利用临时实例销毁调用析构函数 { CMyString strtemp(str); char* ptemp = strtemp.m_pData; strtemp.m_pData = m_pData; m_pData = ptemp; } return *this; } CMyString::CMyString(char * pData) { if (pData==nullptr) { m_pData = new char[1]; m_pData = '\0'; } else { int length = strlen(pData); m_pData = new char[length+1]; strcpy_s(m_pData,length+1, pData); } } CMyString::CMyString(const CMyString & str) { int length = strlen(str.m_pData); m_pData = new char[length+1]; strcpy_s(m_pData,length+1 ,str.m_pData); } CMyString::~CMyString() { delete[] m_pData; } void CMyString::Print() { printf("%s", m_pData); } // ====================测试代码==================== void Test1() { printf("Test1 begins:\n"); char* text = "Hello world"; CMyString str1(text); CMyString str2; str2 = str1; printf("The expected result is: %s.\n", text); printf("The actual result is: "); str2.Print(); printf(".\n"); } // 赋值给自己 void Test2() { printf("Test2 begins:\n"); char* text = "Hello world"; CMyString str1(text); str1 = str1; printf("The expected result is: %s.\n", text); printf("The actual result is: "); str1.Print(); printf(".\n"); } // 连续赋值 void Test3() { printf("Test3 begins:\n"); char* text = "Hello world"; CMyString str1(text); CMyString str2, str3; str3 = str2 = str1; printf("The expected result is: %s.\n", text); printf("The actual result is: "); str2.Print(); printf(".\n"); printf("The expected result is: %s.\n", text); printf("The actual result is: "); str3.Print(); printf(".\n"); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); return 0; }
相关文章推荐
- 剑指offer面试题3-二维数组查找问题
- 剑指offer--面试题49:把字符串转换成整数
- 剑指Offer面试题12[打印1到最大的n位数]
- 剑指offer-面试题40-数组中只出现一次的数字
- 面试题04_替换空格_剑指Offer系列
- 剑指offer 面试题06. 从尾到头打印链表
- 剑指offer系列-面试题37-序列化二叉树(python)
- 剑指offer-面试题 15:链表中倒数第 k 个结点
- 剑指offer-chapter3-面试题17-合并两个排序的链表(java)
- 《剑指offer》面试题29:顺时针打印矩阵
- 《剑指offer》 面试题30. 包含min函数的栈
- 剑指offer面试题14:调整数组位置使奇数位于偶数前面
- 剑指Offer:面试题24——二叉搜索树的后序遍历序列(java实现)
- 剑指Offer:面试题22 栈的压入、弹出序列
- 剑指offer面试题14
- 剑指offer面试题18:树的子结构
- 《剑指offer》面试题3:二维数组中的查找
- 剑指Offer面试题10 & Leetcode191
- 剑指offer 面试题
- (剑指Offer)面试题17:合并两个排序的链表