类 String 的构造函数、析构函数和赋值函数
来源:互联网 发布:北京专业数据恢复 编辑:程序博客网 时间:2024/05/17 05:19
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::String(const char *str){if (str == NULL){m_data = new char[1] ;*m_data = '\0' ;}else{int length = strlen(str) ;m_data = new char[length + 1] ;strcpy(m_data , str) ;}}//拷贝构造函数String::String(const String &other ){int length = strlen(other.m_data) ;m_data = new char[length + 1] ;strcpy(m_data , other.m_data) ;}//赋值函数String &String::operator=(const String &other){if (this == &other) //检查自赋值{return *this;}delete []m_data ; //释放原有的内存//分配新的内存资源, 并复制内容int length = strlen(other.m_data) ;m_data = new char[length + 1] ;strcpy(m_data , other.m_data) ;//返回本对象的引用return *this;}char *strcpy(char *strDest , const char *strSrc){assert((strDest != NULL) && (strSrc != NULL)) ;char *p = strDest ;while((*strDest++ = *strSrc++) != '\0');return p;}
0 0
- string类的构造函数,析构函数,拷贝构造函数和赋值构造函数
- String 的构造函数、析构函数和赋值函数
- string的构造函数,析构函数和赋值函数
- string类的构造函数,拷贝构造函数,析构函数和赋值函数
- String类的构造函数,析构函数,拷贝构造函数和赋值函数
- string类的构造函数,拷贝构造函数,析构函数和赋值函数
- 编写类String的构造函数、拷贝构造函数、析构函数和赋值函数
- string类的构造函数、析构函数、拷贝构造函数和赋值函数
- 编写类String 的构造函数、拷贝构造函数、析构函数和赋值函数
- 写String类的普通构造函数,析构函数,拷贝构造函数和赋值函数
- 类String的构造函数,析构函数,赋值函数
- String的构造函数、析构函数和赋值数
- 网易游戏笔试题:编写string类的构造函数、拷贝构造函数、赋值构造函数和析构函数
- String的构造函数,拷贝构造函数、析构函数和赋值函数
- String构造函数,析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 编写类String的构造函数,析构函数和赋值函数
- 编写类String的构造函数、析构函数和赋值函数
- 莎莎
- 无锡是不是也有一个皮革城啊?
- 欧莱雅的矿物质粉今天去买回来了
- 各位JM们介绍下~过几天去杭州啊
- ~想买外套啊~~~
- 类 String 的构造函数、析构函数和赋值函数
- HDU 2553 N皇后问题
- linux C 编程生成uuid码
- C++解除文件占用
- 所谓爱情不爱情,已经不重要了,信任与否,也不是最关键的了
- 女人要学会的四种气质
- MM们,看看这些婚纱怎么样
- 虹桥南路的那个偶然名品屋
- 跟大家推荐一种不错的普尔美人茶