Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing the content of the shared pointer?

I have a shared pointer that I have shared around the system. Later on, I want to replace the actual content of these shared pointers point to, but still keep all the shared pointers valid, so they internally point to this new object.

Is there an easy way to do this with shared pointers?

Kind of like this I am looking for - pseudo-code

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

EDIT: I want the effect of this, but less gimicky

ModelPtr model1 = ModelPtr(new Model("monkey"));
memcpy(model1 .get(), new Model("donkey"), sizeof(Model));
like image 475
KaiserJohaan Avatar asked Jul 14 '13 17:07

KaiserJohaan


1 Answers

If I understood you correctly - you could use overloaded dereference operator:

auto a = std::make_shared<int>(42);
auto b = a;
*a = 43;

std::cout << *b << std::endl;

Output:

43
like image 172
awesoon Avatar answered Nov 15 '22 18:11

awesoon