You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In create function, if new char[this->buf_len]{}; throws an exception, *this won't keep a valid state. So, copy assignment operator is not exception safe.
The solution is copy the underlying data firstly, then delete *this's old resource:
CPP/week11/examples/example2/mystring.hpp
Lines 29 to 48 in 8d45044
Issue1: can't handle self-assignment
See: Assignment Operators, C++ FAQ (isocpp.org)
If
x = x
, bad errors will occur.We can handle self-assignment by explicitly testing for self-assignment:
Issue2: not exception safe
In
create
function, ifnew char[this->buf_len]{};
throws an exception,*this
won't keep a valid state. So, copy assignment operator is not exception safe.The solution is copy the underlying data firstly, then delete
*this
's old resource:See also
The text was updated successfully, but these errors were encountered: