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

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

//string???????????????????????????//??????String::String(const char *str) {    if (str == NULL) {        //???????????????'\0'???        m_data = new char[1];        *m_data = '\0';    } else {        int length = strlen(str);        m_data = new char[length + 1];        //??? NULL ?????        if (m_data == nullptr) {            cout << "out of space!";            //???????????        }    }}//??????String::String(const String &other) {    //???????    m_data = new char(other.length() + 1);    memcpy(m_data, other.m_data, other.length());    m_data[other.length()] = '\0';}//????String::~String() {    //?????????    delete[] m_data;}//???????String &String::operator=(const String &other) {    //???????????????????    if (this == &other) {        return *this;    }    //?????????    delete[] m_data;    //?????????    m_data = new char(other.length() + 1);    memcpy(m_data, other.m_data, other.length());    m_data[other.length()] = '\0';    return *this;}

//?????//1. ?????????????????????????????//2. ???????????????//3. ????????????????//4. ???new?delete??????????????//5. ??????memcpy???????????//6. ????????????????unique_ptr???RAII??

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

你可能感兴趣的文章
Python 接口自动化 —— requests框架
查看>>
python 接口自动化数据结构(如列表、字典、元组)
查看>>
Python 接口自动化测试中的深拷贝与浅拷贝~
查看>>
Python 接口自动化测试中的高阶函数
查看>>
python 控制 cmd 命令行颜色
查看>>
Python 操作 Azure Blob Storage
查看>>
Python 操作 Excel 全攻略 | 包括读取、写入、表格操作、图像输出和字体设置
查看>>
Python 操作 JMeter 探索:pymeter 实操指南
查看>>
Python 操作 Redis
查看>>
Python 操作Mysql(PyMysql)
查看>>
Python 操作Redis
查看>>
Python 操作sqlite数据库及保存查询numpy类型数据(一)
查看>>
Python 操作sqlite数据库及保存查询numpy类型数据(二)
查看>>
Python 操作数据库
查看>>
Python 数据分析与可视化实战
查看>>
python 数据可视化 -- matplotlib02
查看>>
python 数据可视化利器
查看>>
Python 数据可视化详解
查看>>
python 数据库查询返回list或tuple
查看>>
Python 数据库连接池管理的基本知识 (附Demo)
查看>>