Re: [問題] operator=裡呼叫destructor

作者: kwpn (ITSST)   2014-11-26 18:06:39
※ 引述《fr3ak (fr3@K)》之銘言:
: ※ 引述《kwpn (ITSST)》之銘言:
: : 除了copy assigment可以用copy and swap idiom,
: : move assignment也可以用。
: : A::A(const A &rhs)
: : : vec()
: : {
: : vec.reverse(rhs.vec.size());
: : for (auto &val : rhs.vec)
: : {
: : vec.push_back(new int(*val));
: : }
: : }
感謝大大發現錯誤。
修正1:
reverse 應改成 reserve,
先前code直接在bbs上打的,一時手X。
修正2:
此constructor為non exception safe,
並且導致A& A::operator=(A rhs)也為non exception safe。
當第二次以後的new int發生exception會導致vec裡的int* memory leak。
以目前的架構可以改成
A::A(const A &rhs)
: vec()
{
vec.reserve(rhs.vec.size());
try {
for (auto &val : rhs.vec)
{
vec.push_back(new int(*val));
}
} catch(...) {
clear_up();
throw;
}
}
其中clear_up()為清除vec。
雖然不知道為啥原po會宣告成std::vector<int*>而不是std::vector<int>,
但若硬是要用pointer,使用smart pointer(std::unique_ptr, std::shared_ptr)
可以省略掉A::A(const A &rhs)裡面的try catch也能達到exception safe。
A& A::operator=(A rhs) copy and swap idiom為exception safe,
前提是copy constructor與move constructor皆為exception safe。
: : A::A(A &&rhs) noexcept
: : : vec(std::move(rhs.vec))
: : {
: : }
: : A& A::operator=(A rhs)
: : {
: : swap(rhs);
: : return *this;
: : }
: : void A::swap(A &rhs) noexcept
: : {
: : std::swap(vec, rhs.vec);
: : }
: 小弟不太嫩, PTT 首 PO.
: 相對於其他支援 exception 的語言, C++ 要求 programmer 對 exception 更有 sense,
: 更敏感.
: 出個小作業給有興趣的朋友一起玩玩: 怎麼修改可以達成 exception safety?
: 提示: 有兩個地方在發生 exception 的時候會 leak.
作者: fr3ak (fr3@K)   2014-11-26 18:11:00
Exceeds expectation不過我假設 int* 代表的是 1. 某種動態 allocated resourceand 2. noexcept assignment所以不是很 care 為什麼是 int*

Links booklink

Contact Us: admin [ a t ] ucptt.com