正从shared_ptr的一个正常的PTR? [英] getting a normal ptr from shared_ptr?

查看:168
本文介绍了正从shared_ptr的一个正常的PTR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的shared_ptr<类型> T(makeSomething(),mem_fun(安培;类型:: DELETEME))
我现在需要调用C风格独具特色的功能,需要一个指向键入。我如何得到它从的shared_ptr

I have something like shared_ptr<Type> t(makeSomething(), mem_fun(&Type::deleteMe)) I now need to call C styled function that requires a pointer to Type. How do I get it from shared_ptr?

推荐答案

另一种方式来做到这将是使用&功放的组合; * 运营商:

Another way to do it would be to use a combination of the & and * operators:

boost::shared_ptr<foo> foo_ptr(new foo());
c_library_function( &*foo_ptr);

虽然我个人倒preFER使用的get()方法(它是真正正确的答案),一个优点,这已是它可以与超载其他类运算符* (指针引用)使用,但不提供的get()方法。可能是通用类模板非常有用,例如

While personally I'd prefer to use the get() method (it's really the right answer), one advantage that this has is that it can be used with other classes that overload operator* (pointer dereference), but do not provide a get() method. Might be useful in generic class template, for example.

这篇关于正从shared_ptr的一个正常的PTR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆