在图书馆的公共接口使用boost :: shared_ptr的 [英] Using boost::shared_ptr in a library's public interface

查看:154
本文介绍了在图书馆的公共接口使用boost :: shared_ptr的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个C ++库,我们提供几个不同的客户端。最近我们从公共接口使用boost :: sharedptr而不是使用原始指针做了开关。这提供了一个巨大的好处,因为你可能已经猜到,在现在的客户再也不用担心谁需要删除的内容和时间。当我们做了开关我相信这是应该做的事情,但它困扰着我,我们必须包括来自第三方库的东西在我们的公共接口 - 通常你避免这种事情如果你能。我合理化,认为提升几乎是C ++语言的一部分,现在,我们的用例需要在客户端code和库保持指向的对象。但是最近我们的一位客户已经要求我们,如果我们可以切换到该接口采用中立的智能指针类,因为我们的图书馆基本上迫使他们以助推点的特定版本,我当然理解和AP preciate 。所以现在我想知道最好的行动当然可能是什么。我曾经想过一点点,不知道如何创建一个简单的智能指针类,简单地举行了真正的助力智能指针。但随后的客户可能会立即这些东西到他们的boost :: sharedptr味道的一个,然后我们会深三共享指针 - 这可能是一个问题,也可能不是。无论如何,我很乐意听到有关解决这一问题的最好方式,从社会的一些看法。

We have a C++ library that we provide to several different clients. Recently we made the switch from using raw pointers in the public interface to using boost::sharedptr instead. This has provided an enormous benefit, as you might guess, in that now the clients no longer have to worry about who needs to delete what and when. When we made the switch I believed it was the right thing to do, but it bothered me that we had to include something from a third-party library in our public interface - generally you avoid that kind of thing if you can. I rationalized it that boost was practically part of the C++ language now, and our use case requires that both the client code and the library hold pointers to the objects. However recently one of our clients has asked us if we could switch to using a neutral smart pointer class in the interface, because our library is essentially forcing them to a particular version of boost- a point which I certainly understand and appreciate. So now I am wondering what the best course of action might be. I have thought about it a little bit, and wondered about creating a simple smart pointer class that simply held a real boost smart pointer. But then the clients would probably immediately stuff one of those into their flavor of boost::sharedptr, and then we'd be three shared pointers deep - which might be a problem, or it might not. Anyway, I'd love to hear some opinions from the community about the best way to solve this problem.

编辑:我原来说所有权的转移,但我应该有指定的API边界的两边code需要举行一个指向对象

I originally said transfer of ownership, but I should have specified that code on both sides of the API boundary need to hold a pointer to the object.

推荐答案

&的shared_ptr LT;>的的语言的一部分,因为TR1的释放。
请参阅:(TR1)

shared_ptr<> is part of the language, as of the release of TR1. See: (TR1)

这篇关于在图书馆的公共接口使用boost :: shared_ptr的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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