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

你可能感兴趣的文章
pwnable bof
查看>>
PXC / Galera Cluster集群概述及原理分析
查看>>
PXC搭建MySQL高可用集群
查看>>
PXE + Kickstart 服务器批量安装Linux系统
查看>>
PXE+HTTP+TFTP+Kickstart实现无人值守部署centos6.10
查看>>
pxe实现linux的自动安装
查看>>
py 的 第 19 天
查看>>
Py-Bitcoin 项目使用教程
查看>>
Pytorch 图像增强 实现翻转裁剪色调等 附代码(全)
查看>>
pyautogui模拟鼠标拖动选中文字的基本知识(附Demo)
查看>>
pyautogui的基本介绍和使用
查看>>
PyCharm - 社区版是否能够突出显示 css/javascript?
查看>>
PyCharm 2018.3.5 RC 发布,原生 SSH 支持
查看>>
PyCharm 3.1 在索引期间永远挂起并且无法使用
查看>>
Pycharm Pro 2018.2 汉化专业激活破解
查看>>
PyCharm vs VSCode,是时候改变你的 IDE 了!
查看>>
PyCharm 中,“新建”(New)和“新建项目”(New Project)-ChatGPT4o作答
查看>>
PyCharm 代码编辑与调试运行详解
查看>>
Pycharm 出现 instantitaing tests 以及test session starts 的解决方法
查看>>
pytorch 固定随机种子
查看>>