博客
关于我
20170825_string构造函数、析构函数、拷贝构造函数以及重载赋值运算符
阅读量:96 次
发布时间: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/

你可能感兴趣的文章
Objective-C实现中值滤波(附完整源码)
查看>>
Objective-C实现中国剩余定理(附完整源码)
查看>>
Objective-C实现中国剩余定理(附完整源码)
查看>>
Objective-C实现中文模糊查询(附完整源码)
查看>>
Objective-C实现串口通讯(附完整源码)
查看>>
Objective-C实现串逐位和(附完整源码)
查看>>
Objective-C实现串链式存储简单匹配(附完整源码)
查看>>
Objective-C实现主存储器空间的分配和回收(附完整源码)
查看>>
Objective-C实现乘方运算---m的n次方(附完整源码)
查看>>
Objective-C实现乘法持续性multiplicative persistence算法(附完整源码)
查看>>
Objective-C实现二分查找最接近的数值m(附完整源码)
查看>>
Objective-C实现二分查找最接近的数值m(附完整源码)
查看>>
Objective-C实现二叉搜索树算法(附完整源码)
查看>>
Objective-C实现二叉树层序遍历(附完整源码)
查看>>
Objective-C实现二叉树遍历算法(附完整源码)
查看>>
Objective-C实现二次方程复数算法(附完整源码)
查看>>
Objective-C实现二维向量以及各种向量操作算法(附完整源码)
查看>>
Objective-C实现二维矩阵运算的函数算法(附完整源码)
查看>>
Objective-C实现二维码(显示+保存图片)功能源代码(附完整源码)
查看>>
Objective-C实现二进制和算法(附完整源码)
查看>>