Lintcode208 Assignment Operator Overloading (C++ Only) solution 題解

weixin_33785972發表於2017-11-04

【題目描述】

Implement an assignment operator overloading method.

Make sure that:

The new data can be copied correctly

The old data can be deleted / free correctly.

We can assign like  A = B = C

實現賦值運算子過載函式,確保:

新的資料可準確地被複制

舊的資料可準確地刪除/釋放

可進行A = B = C賦值

【題目連結】

www.lintcode.com/en/problem/assignment-operator-overloading-c-only/

【題目解析】

這題就是考c++中的overload。題目要求先delete A,再copy B中的data。那麼首先檢查A的data是不是已經等於B了,如果是,直接return;如果不是,先delete A的data,然後new一個新的char*,把B的data copy到A中去。

【參考答案】

www.jiuzhang.com/solutions/assignment-operator-overloading-c-only/

相關文章