有沒有辦法將代碼寫入方法的簽名中,無論對象所有權是否更改?在獲取或返回指針的Getter()和Setter()中,您永遠不知道對象所有權是否更改。方法中對象所有權的命名約定
你怎麼看待:
// Uses pConfiguration or creates its own copy - the ownership is un-touched
void ContainerClass:SetConfiguration(const Configuration* pConfiguration) {}
// Takes the ownership of pConfiguration (and deletes it in the destructor)
void ContainerClass:PutConfiguration(Configuration* pConfiguration) {}
// Returns a 'reference' and keeps the ownership (you must not delete it)
const Configuration* ContainerClass::GetConfiguration() {}
// Returns a _new_ object and transfers the ownership to you
Configuration* ContainerClass::TakeConfiguration() {}
所以被設置()與PUT()和get()與以()的方式來編碼,或者你可以使用類型(常量與非const) - 或者你是否從上下文知道它?
最好的問候,
查理
你檢查過智能指針嗎?它將避免這些類型的問題。 – Naveen 2011-04-01 07:26:59
嗯......它解決了刪除問題...但它仍然沒有提到對象所有權。我必須考慮 - 如果它解決了我所有的問題:) – Charly 2011-04-01 07:33:06