2013-04-09 110 views
1

這是合乎邏輯的,在棧上創建對象...對象的副本返回,原來被刪除爲什麼不調用析構函數?

Box operator +(const Box& box) const 
{ 
    Box b = Box(this->num + box.num); 
    return b;       
} // destructor called! 

爲什麼在這種情況下是不同的過程?

Box operator +(const Box& box) const 
{ 
    return Box(this->num + box.num); 
} // destructor not called! 

爲什麼在第二個運算符重載方法中不調用析構函數?

+0

你是如何使用這個功能的?你分配結果? – 2013-04-09 19:00:38

+0

Box box1 = box2 + box3; – 2013-04-09 19:01:16

+0

查找「返回值優化」。 – Angew 2013-04-09 19:02:14

回答

相關問題