CodingInterviewChinese2 icon indicating copy to clipboard operation
CodingInterviewChinese2 copied to clipboard

面试题1考虑异常安全的解法有误

Open pusidun opened this issue 5 years ago • 4 comments

代码仓库没有给出,书上给出了

//copy operator
CMyString& CMyString::operator=(const CMyString& str)
{
	if(m_pData != &str)
	{
		CMyString tmpData(str);
		
		char *ptmp = tmpData.m_pData;
		tmpData.m_pData = m_pData;
		m_pData = ptmp;
	}
	return *this;
}

m_pData是private的,书上写的有点问题

pusidun avatar Aug 04 '19 15:08 pusidun

private 在这里没影响吧

zhiqiang-hhhh avatar Nov 13 '19 12:11 zhiqiang-hhhh

@hezhiqiangTS tmpData.m_pData 这里,private不能这么写吧

pusidun avatar Nov 17 '19 06:11 pusidun

private 在这里没影响吧

不懂,可以解释一下吗?

acxmx avatar May 14 '20 00:05 acxmx

同问,tmpData不能直接访问它的private成员吧?需要通过成员函数访问的吧? 但是自己写代码测试也是能通过,现在很疑惑,求懂的同学解答!

acxmx avatar May 14 '20 00:05 acxmx