编写类String的构造函数、析构函数和赋值函数
来源:互联网 发布:淘宝营销推广方式 编辑:程序博客网 时间:2024/05/20 16:00
具体代码:
class String{public:String(const char *str = NULL);//默认构造函数String(const String &other);//赋值构造函数~String(void);String & operator =(const String & other);//赋值函数private:char *m_data;};String::String(const char *str ){if ( str != NULL ){int len = strlen(str);m_data = new char[len+1];strcpy(m_data,str);}else//这部分不要拉掉{m_data = new char[1];*m_data = '\0';}}String::String(const String &other)//赋值{int len = sizeof(other.m_data);m_data = new char[len+1];strcpy(m_data, other.m_data);}String & String::operator =(const String &other){//1:检测是否是自赋值if ( this == &other )return *this;//2:释放原有的内存资源delete [] m_data;//3:分配新的内存资源,并复制内容int len = strlen(other.m_data);m_data = new char[len+1];strcpy(m_data, other.m_data);//4:返回本对象的引用return *this;}String::~String(void){delete [] m_data;}
- 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数
- 编写类String 的构造函数、拷贝构造函数、析构函数和赋值函数
- 网易游戏笔试题:编写string类的构造函数、拷贝构造函数、赋值构造函数和析构函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数,析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String 的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 如何正确编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String 的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 海量数据面试题举例
- jQuery学习08---相对定位,工资点击求和
- 我要开始写博客了。
- C++11智能指针shared_ptr的向上转upcast向下转downcast
- 修改Flash Builder注释里的@author
- 编写类String的构造函数、析构函数和赋值函数
- 素数环
- Tomcat部署Web应用方法总结
- .NET Framework小结
- MS Access
- dnet iis7发布mvc4 报 403错误解决办法
- Android CTS 结果 testResult.xml 修改 fail 项 为 notExecuted 项 分析
- wamp mysql控制台
- 软件工程管理---演化与总结