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

你可能感兴趣的文章
php九九乘法表加粗,PHP九九乘法表
查看>>
PHP二维数组将重复键值合并重组成三维数组
查看>>
PHP二维数组转换为一维数组
查看>>
PHP二维数组重组
查看>>
PHP交换两个变量值
查看>>
php代码执行完整流程介绍
查看>>
PHP代码格式化工具phpcf常见问题解决方案
查看>>
PHP使用3DES算法加密解密字符串
查看>>
PHP使用curl multi要注意的问题:每次使用curl multi同时并发多少请求合适
查看>>
php使用memcached扩展的一个BUG
查看>>
SpringBoot基础教程2-1-11 RestTemplate整合HttpClient
查看>>
PHP入门part1
查看>>
PHP兼容性检查,PHP升级语法检查(PHPCompatibility+PHP_CodeSniffer)
查看>>
PHP内核介绍及扩展开发指南—基础知识
查看>>
php内核基础说明
查看>>
PHP写日志fwrite和file_put_contents的区别与性能
查看>>
PHP写计划任务
查看>>
PHP出现Notice: unserialize() [function.unserialize]: Error at offset问题的解决方案
查看>>
PHP函数
查看>>
React input defaultValue不会更新状态怎么办?
查看>>