关于boost :: shared_ptr的混乱 [英] Confusion concerning boost::shared_ptr

查看:242
本文介绍了关于boost :: shared_ptr的混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题在于是否必须暴露我从我的界面使用boost :: shared_ptr,以及是否应该从我的界面暴露原始指针或引用。

My question revolves around whether or not I must expose my use of the boost::shared_ptr from my interface and whether or not I should expose raw pointers or references from my interface.

考虑有雇员的情况。 Employeer 内部维护其所有员工在向量< shared_ptr<人物> > 。因此,最好的做法是,任何涉及Person的接口都应该是shared_ptr包装的人吗?

Consider the case of a Person who has an Employeer. Employeer internally maintains all of its employees in a vector< shared_ptr< Person > >. Because of this, do best practices dictate that any interface involving Person should be a shared_ptr wrapped person?

例如,全部或部分是正确的:

For example, are all or only some of these ok:

Person Employeer::getPresidentCopy();
Person& Employeer::getPresidentRef();
Person* Employeer::getPresidentRawPtr();
shared_ptr<Person> Employeer::getPresidentSharedPtr();

或例如:

void Employeer::hireByCopy(Person p);
void Employeer::hireByRef(Person& p);
void Employeer::hireByRawPtr(Person* p);
void Employeer::hireBySharedPtr(shared_ptr<Person> p);

如果我以后要将实现更改为使用johns_very_own_shared_ptr而不是boost类型,旧的实现?

If I later want to change the implementation to use johns_very_own_shared_ptr instead of the boost variety, am I trapped in the old implementation?

另一方面,如果我暴露来自接口的原始指针或引用,我冒险从别人的shared_ptr下删除内存?

On the other hand, if I expose raw pointers or references from the interface, do I risk someone deleting the memory out from under the shared_ptr? Or do I risk the shared_ptr being deleted and making my reference invalid?

请参阅我的新问题中涉及此问题的示例。

See my new question for an example involving this.

推荐答案


例如,是全部还是只有一些确定:

For example, are all or only some of these ok:

这取决于你试图完成。为什么向量持有 shared_ptr s而不是直接按值存储 Person s? (你有没有考虑过 boost :: ptr_vector ?)

It depends on what you're trying to accomplish. Why does the vector hold shared_ptrs instead of just directly storing Person s by value? (And have you considered boost::ptr_vector?)

你也应该考虑一下你真正应该out out是 weak_ptr

You should also consider that maybe what you really ought to hand out is a weak_ptr.


如果我以后要将实现更改为使用johns_very_own_shared_ptr而不是增强品种,我陷入了旧的实现?

If I later want to change the implementation to use johns_very_own_shared_ptr instead of the boost variety, am I trapped in the old implementation?

相当多,但它不是不可能修复。 (我怀疑在C ++ 0x,自由使用 auto 关键字将使这更容易处理,因为你不必修改调用代码多少,即使它没有使用 typedef 。)但是,为什么你会希望这样做?

Pretty much, but it's not impossible to fix. (I suspect that in C++0x, liberal use of the auto keyword will make this easier to deal with, since you won't have to modify the calling code as much, even if it didn't use typedef s.) But then, why would you ever want to do that?


另一方面,如果我暴露来自接口的原始指针或引用,那么是否有人冒险从shared_ptr下删除内存?

On the other hand, if I expose raw pointers or references from the interface, do I risk someone deleting the memory out from under the shared_ptr?

是的,但这不是你的问题。人们可以从 shared_ptr delete 中提取原始指针。但是如果你想避免使事情不必要的不​​安全,不要在这里返回原始指针。参考文件更好,因为没有人能够应该 delete& reference_received_from_api; 。 (我希望如此,无论如何^^ ;;;;)

Yes, but that's not your problem. People can extract a raw pointer from a shared_ptr and delete it, too. But if you want to avoid making things needlessly unsafe, don't return raw pointers here. References are much better because nobody ever figures they're supposed to delete &reference_received_from_api;. (I hope so, anyway ^^;;;; )

这篇关于关于boost :: shared_ptr的混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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