1
我有一個情況,在那裏我有現有的與原始指針運行的代碼,而我不允許智能指針IFY它。但是,我被允許在我開發的任何新代碼中使用智能指針。集成智能指針遺留代碼原始指針
例如。
我有一個現有的功能,如:
void processContent()
{
ContentObject * myContent = new ContentObject();
newFunction(myContent);
}
void newFunction(ContentObject * content)
{
// myVector is just a std::vector<ContentObject*>, defined elsewhere
myVector.push_back(content);
}
void doSomethingWithContent()
{
// There is some logic here, but ultimately based on this logic I want to remove entries, and free the memory they point to.
myVector.pop_back();
}
我有過 「newFunction」 和 「doSomethingWithContent」 的內容控制。但傳遞給newFunction的參數是固定的。很明顯,我可以在彈出它之前手動刪除myVetor中的指針,但是我想知道我是否可以在這裏實現智能指針,以便「自動」爲我執行?
我可以帶傳遞給函數的原始指針,並把它變成一個的unique_ptr,再加入這一個容器,並當它從容器中彈出它刪除記憶?
感謝
喬伊