views:

48

answers:

1

Common pointers allows you to create pointer to pointer:

void foo(Object **o) {}

int main()
{
   Object * o = new Object();
   foo(&o);
}

Is there an analogous construction for shared_ptr?

void foo(std::shared_ptr <Object> *o) {}

int main()
{
   std::shared_ptr <Object>  o(new Object());
   foo(&o);
}
+2  A: 

Without testing this, I'm pretty sure you'd want:

shared_ptr<shared_ptr<T> > o(new shared_ptr<T>(new T()));

Edit: forgot the "new" after the first '(', and fixed non-standard use of >> in a template definition. (at least, not standard until C++0x)

Ken Simon
And of course, the signature for foo() would be: void foo(std::shared_ptr<std::shared_ptr<Object> > o);
Ken Simon
Surely he's looking for something simpler such as parameter: `shared_ptr<T>* p` and implementation `p->reset(new T)` . Although even this looks clumsy to me.
Charles Bailey