题目一:赋值运算符函数


/////////////////////////////////////////////////////////////////////////////////////////////
// 3.题目一:赋值运算符函数
class CMyString
{
public:
    // 默认函数
    CMyString();
    CMyString(const CMyString&);
    CMyString& operator=(const CMyString&);
    ~CMyString();

    void SetString(const char* pszStr)
    {
        // 释放旧数据
        Destroy();
        CopyData(pszStr);
    }

    void Print()
    {
        if (m_pszData)
        {
            cout << m_pszData << endl;
        }
    }

    void Destroy()
    {
        if (m_pszData)
        {
            delete [] m_pszData;
            m_pszData = NULL;
        }
    }

    void CopyData(const char* pszStr)
    {
        if (pszStr)
        {
            int iLen = strlen(pszStr);
            m_pszData = new char[iLen + 1];
            strncpy(m_pszData, pszStr, iLen + 1);
            m_pszData[iLen] = '\0';
        }
    }

private:
    char* m_pszData;
};

// 默认构造函数
CMyString::CMyString()
    :m_pszData(NULL)
{

}

//拷贝构造函数
CMyString::CMyString(const CMyString& other)
{
    cout << "CMyString Copy Constructor!!!!" << endl;
    // 释放旧数据
    //Destroy();    // 这里不能释放,因为没有初始化

    CopyData(other.m_pszData);
}

//赋值操作符
CMyString& CMyString::operator=(const CMyString& other)
{
    cout << "CMyString Assignment Operator!!!!" << endl;

#if 0   // 初级程序员!!!!
    if (this != &other)
    {
        Destroy();
        CopyData(other.m_pszData);
    }

#else 
    //高级程序员 需要考虑异常 --> 内存不足,导致new char 抛出异常
    // 实现异常安全性:
    // 1.先new分配新内容在使用delete释放旧内容 --> 确保抛出异常时原来的数据不会被改变!!!
    // 2.先创建一个临时实例,再交换临时实例和原来的内容  --> 推荐这种方法!!!!
    if (this != &other)
    {
        // 调用拷贝构造函数创建临时实例
        CMyString strTmp(other);
        char * pTmp = strTmp.m_pszData;
        strTmp.m_pszData = m_pszData;   // strTmp调用析构函数时,会释放原来实例中的m_pszData!!!!
        m_pszData = pTmp;
    }

#endif
    return *this;
}

//析构函数
CMyString::~CMyString()
{
    Destroy();
}

void MyStringTestFunc()
{
    cout << "\n\n --------------- MyStringTestFunc Start -------------->" << endl;

    CMyString str1;
    str1.SetString("Hello World!");
    str1.Print();

    CMyString str2(str1);   // 调用拷贝构造函数
    str2.Print();

    CMyString str3 = str1;  // 调用拷贝构造函数 等同于CMyString str3(str1)
    str3.Print();

    CMyString str4;
    str4 = str1;            // 赋值操作符
    str4.Print();


    cout << "\n\n --------------- MyStringTestFunc End -------------->" << endl;

}
posted @ 2019-07-28 13:03  VIP丶可乐  阅读(214)  评论(0编辑  收藏  举报