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

你可能感兴趣的文章
php 数组 区别,PHP中数组的区别
查看>>
php 生成csv mac下乱码
查看>>
php中的session用法
查看>>
php之aop实践
查看>>
PHP二维数组将重复键值合并重组成三维数组
查看>>
PHP二维数组转换为一维数组
查看>>
PHP交换两个变量值
查看>>
PHP代码格式化工具phpcf常见问题解决方案
查看>>
PHP学习总结(12)——PHP入门篇之变量
查看>>
PHP学习总结(4)——PHP入门篇之PHP计算表达式
查看>>
PHP学习笔记一:谁动了你的mail(),PHP?
查看>>
PHP安全实战
查看>>
php安装扩展
查看>>
php实现根据身份证获取年龄
查看>>
PHP实现的MongoDB数据增删改查
查看>>
PHP对表单提交特殊字符的过滤和处理
查看>>
PHP将图片转换成base64格式(优缺点)
查看>>
php将多个值的数组去除重复元素
查看>>
php局域网上传文件_PHP如何通过CURL上传文件
查看>>
PHP工具插件大全
查看>>