C++赋值函数一些考虑
来源:互联网 发布:淘宝banner图 编辑:程序博客网 时间:2024/06/06 08:34
// Primary programmer use this wayCMyString & CMyString::operator=(const CMyString & str) {if(this == &str)return *this;delete [] m_pData;m_pData = NULL;/** Here may be failed. If this step failed, and next step will* failed too. And the pointer m_pData will not point to the* original space it will point to NULL. It's so easily lead to crashes.*/m_pData = new char[strlen(str.m_pData) + 1];strcpy(m_pData, str.m_pData);return *this;}// The better wayCMyString & CMyString::operator=(const CMyString & str){if(this != &str){/** strTemp is local variable just in scope of "if", so when * program run out of the scope of "if", strTemp will call* destructor free the area that strTemp.m_pData point at.*/CMyString strTemp(str); char *strTemp = strTemp.m_pData;/* * so the memory where m_pData point at will been free * by strTemp.m_pData with program run out of the scope * of "if" */strTemp.m_pData = m_pData; m_pData = pTemp;}return *this;}
0 0
- C++赋值函数一些考虑
- c 语言。函数赋值
- c函数赋值
- 以后写函数要考虑多一些
- 【C++】赋值运算符函数
- 【C++】拷贝构造函数和赋值函数
- 复制构造函数与赋值重载(c++)
- 实现C++string的赋值函数
- 一些c函数
- 一些C函数
- 一些C函数
- C中的一些函数
- 一些c函数
- C语言一些函数
- 【C语言】模拟实现memmove函数(考虑内存重叠)
- 《Effective C++》35:考虑virtual函数以外的其他选择
- 【C/C++】拷贝构造函数和赋值函数
- 赋值运算符重载函数[C/C++/C#]
- redhat-linux 学习4:虚拟机网络配置说明
- Android开发——Android的消息机制详解
- 拷贝构造函数和赋值函数
- caused by android.system.errnoexception open failed eacces (permission denied)解决方案,安卓6.0(API23)权限问题
- Struts2常用标签
- C++赋值函数一些考虑
- 编程问题反馈/提问原则:
- H264—帧,片,参数集,NALU等概念
- 工作第八周:转正啦哈哈!顺便立些 flag
- 树链剖分
- Triangle
- Android简易实战教程--第二十四话《画画板》
- linux添加定时任务
- HTML5-CSS3