我有共享的指針,我已經在系統中共享。稍後,我想將這些共享指針的實際內容替換爲指向,但仍然保持所有共享指針有效,因此它們在內部指向此新對象。替換共享指針的內容?
有一種簡單的方式與共享指針做到這一點?
有點像這個我要找 - 僞代碼
typedef boost::shared_ptr<Model> ModelPtr
ModelPtr model1 = ModelPtr(new Model);
ModelPtr model2 = model1;
// make something like 'model1.get() = new Model' so model1, model2 both points to a new model
編輯: 我想這樣的效果,但不太gimicky
ModelPtr model1 = ModelPtr(new Model("monkey"));
memcpy(model1 .get(), new Model("donkey"), sizeof(Model));
也許你可以使用'shared_ptr的>'? –
rodrigo
'* model1 = Model {};'做你需要的嗎? – aschepler