私は、推奨される C++ の本の 1 つを読んでいました。
オブジェクトがそれ自体に割り当てられている場合でも、割り当て演算子が正しく機能することが非常に重要です。これを行う良い方法は、左側のオペランドを破棄する前に右側のオペランドをコピーすることです。
本の例; クラスには 1 つのデータ メンバーがps
あり、ps is string *
C& operator=(const C &rhs)
{
auto newp = new string(*rhs.ps)
delete ps;
ps = newp;
return *this;
}
しかし、私たちのインストラクターは提案しました
C& operator=(const C &rhs)
{
if (this == &rhs)
return *this;
delete ps;
ps = new string(*rhs.ps)
return *this;
}
講師の対応に問題はないか