赋值运算符函数

时间:2022-07-14 01:40:07
class Solution {
public:
    char *m_pData;
    Solution() {
        this->m_pData = NULL;
    }
    Solution(char *pData) {
        this->m_pData = pData;
    }

    // Implement an assignment operator
    Solution operator=(const Solution &object) {
        // write your code here
        if(this == &object)
            return *this;
        
        delete m_pData;
        
        m_pData = NULL;
        
        if(!object.m_pData) return *this;
        
        m_pData = new char[strlen(object.m_pData)];//此处不管加不加1都不能处理 m_pData = NULL 情况 strcpy不支持
        strcpy(m_pData, object.m_pData);
        
        return *this;
    }
};