博客
关于我
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基础之面向对象2(封装)
查看>>
Python基础之给函数增加元信息
查看>>
python基础之字符编码
查看>>
python基础之多线程与多进程(二)
查看>>
Python基础之基本数据类型一《数字与字符串》
查看>>
python基础之变量---总纲
查看>>
Python基础之列表
查看>>
Python基础之Python环境搭建
查看>>
python基础之--面相对象--OOP基本特性
查看>>
python基础之--迭代器和生成器及异常处理
查看>>
python基础之--包和模块
查看>>
python基础之---面向对象--属性和方法
查看>>
python基础之---运算符
查看>>
python基础之---语句
查看>>
python基础之---正则表达式
查看>>
python基础之---函数式编程
查看>>
python基础之---函数
查看>>
Python基础之(一)基本数据类型
查看>>
Python基础【os】
查看>>
Python基础——面向对象
查看>>