我需要使用RAII成語,我在做正確的事:?如何使用std :: auto_ptr作爲函數的參數?
std::auto_ptr<std::vector<string>> MyFunction1()
{
std::auto_ptr<std::vector<string>> arrayOfStrings;
MyFunction2(arrayOfStrings); // work with arrayOfStrings
return arrayOfStrings;
}
void MyFunction2(std::auto_ptr<std::vector<string>> array)
{
auto_ptr<string> str;
*str = "foo string";
array.push_back(str)
}
或許shoudl我自己釋放內存,而不是使用智能指針?如果是這樣,該怎麼做?提前致謝。
只需注意:'std :: auto_ptr'現在已被棄用。它試圖在移動語義存在之前實現移動語義。看看'std :: unique_ptr',它正確地做到了。 –
看看'std :: shared_ptr',那應該解決這個問題。 – Zaffy