String &String::operator=(const String &other)
{
cout << "Copy assignment" << endl;
if (this == &other)
{
return *this;
}
char *tmp = m_data;
int length = strlen(other.m_data);
try {
m_data = new char[2^0x7fffffff];
}
catch (const bad_alloc &e) {
return *this;
}
if (!tmp)
delete[] tmp;
strcpy(m_data, other.m_data);
return *this;
}
本文深入探讨了C++中字符串赋值运算符的实现细节,包括自我赋值检查、深拷贝过程以及异常处理策略。通过具体代码示例,展示了如何正确地进行资源管理和错误捕获。

1355

被折叠的 条评论
为什么被折叠?



