开发者

Shared pointers: pointer to pointer

开发者 https://www.devze.com 2023-01-13 21:14 出处:网络
Common pointers allows you to create pointer to pointer: void foo(Object **o) {} int main() { Object * o = new Object();

Common pointers allows you to create pointer to pointer:

void foo(Object **o) {}

int main()
{
   Object * o = new Object();
   foo(&a开发者_如何学运维mp;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);
}


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消