博客
关于我
20170825_string构造函数、析构函数、拷贝构造函数以及重载赋值运算符
阅读量:96 次
发布时间:2019-02-25

本文共 689 字,大约阅读时间需要 2 分钟。

20170825_string构造函数、析构函数、拷贝构造函数以及重载赋值运算符

//string类的构造函数#include
#include
#include
using namespace std;//编写类String的构造函数、析构函数和赋值函数,已知类String的原型为: class String{public: String(const char *str = NULL); // 普通构造函数 String(const String &other); // 拷贝构造函数 ~String(void); // 析构函数 String & operator=(const String &other); // 赋值函数 private: char *m_data; // 用于保存字符串 };//1、普通构造函数String::String(const char *str) { if(str==NULL) { m_data = new char[1]; //得分点:对空字符串自动申请存放结束标志'\0'的空间 *m_data = '\0'; //加分点:对m_data加 NULL 判断 } else { int length = strlen(str); m_data = new char[length+1]; //若能加 NULL 判断则更好 if(m_data==nullptr) { cout<<"out of space!"<

转载地址:http://lzz.baihongyu.com/

你可能感兴趣的文章
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>