博客
关于我
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/

你可能感兴趣的文章
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>
mysql5.5和5.6版本间的坑
查看>>