简体   繁体   English

返回C ++多态对象(接口)

[英]Returning C++ polymorphic objects (interfaces)

I'd like to know what is considered nowadays the best practice when returning a pointer to a polymorphic object from a function, for example when using factories. 我想知道当今从函数返回指向多态对象的指针时(例如在使用工厂时)的最佳实践 If I transfer the ownership, should I return boost::unique_ptr<Interface> ? 如果我转移所有权,是否应该返回boost::unique_ptr<Interface> What should I return if I don't transfer the ownership (eg returning a reference to a member)? 如果我不转移所有权(例如,返回对成员的引用),应该返回什么? Is there an alternative, non boost-based way which is also commonly used? 是否有一种通常也使用的非基于Boost的替代方法? Thanks. 谢谢。

EDIT: it is supposed to be C++03 compatible, with a possibility to easily upgrade to 0x 编辑:应该是C ++ 03兼容,有可能很容易升级到0x

EDIT2: Please note I'm explicitly asking about common approaches, best practices, and not just "a way to do this". EDIT2:请注意,我明确询问的是通用方法,最佳实践,而不仅仅是“一种实现方法”。 A solution implying a conditional search-and-replace over the codebase in future does not look like a good practice, does it? 暗示将来有条件在代码库中进行有条件搜索和替换的解决方案似乎不是一种好习惯,是吗?

EDIT3: Another point about auto_ptr is that it is deprecated, whatever neat it is, so it looks strange to advertise its usage at the interface level. EDIT3:关于auto_ptr的另一点 ,无论它多么整洁,它都已弃用,因此在接口级别上宣传其用法看起来很奇怪。 Then, someone unaware will put the returned pointer into a STL container, and so on and so forth. 然后,没有意识到的人会将返回的指针放入STL容器中,依此类推。 So if you know another somehow common solution, you are very welcome to add an answer. 因此,如果您知道另一种常见的解决方案,欢迎您添加答案。

Use ::std::auto_ptr for now, and when C++0x is available, then switch to ::std::unique_ptr . 现在使用::std::auto_ptr ,并且在C ++ 0x可用时,然后切换到::std::unique_ptr At least in the factory case where you are handing ownership back to the caller. 至少在工厂情况下,您要将所有权交还给呼叫者。

Yes ::std::auto_ptr has problems and is ugly. 是的::std::auto_ptr有问题并且很丑。 Yes, it's deprecated in C++0x. 是的,在C ++ 0x中已弃用。 But that is the recommended way to do it. 但这是推荐的方法。 I haven't examined ::boost::unique_ptr , but without move semantics I don't see that it can do any better than ::std::auto_ptr . 我没有检查::boost::unique_ptr ,但是没有移动语义,我看不出它可以比::std::auto_ptr做得更好。

I prefer the idea of upgrading by doing a search and replace, though there are some unusual cases in which that won't have the expected result. 我更喜欢通过搜索和替换来进行升级的想法,尽管在某些不常见的情况下,它们不会获得预期的结果。 Fortunately, these cases generate compiler errors: 幸运的是,这些情况会产生编译器错误:

::std::auto_ptr<int> p(new int);
::std::auto_ptr<int> p2 = p; 

will have to become at least like this 至少必须像这样

::std::unique_ptr<int> p(new int);
::std::unique_ptr<int> p2 = ::std::move(p);

I prefer search and replace because I find that using macros and typedefs for things like this tend to make things more obscure and difficult to understand later. 我更喜欢搜索和替换,因为我发现对此类事情使用宏和typedef会使事情变得更加晦涩难懂,以后很难理解。 A search and replace of your codebase can be applied selectively if need be ( ::std::auto_ptr won't go away in C++0x, it's just deprecated) and leaves your code with clear and obvious intent. 如果需要,可以有选择地应用代码库的搜索和替换( ::std::auto_ptr在C ++ 0x中不会消失,只是不推荐使用),并且使代码具有清晰明显的意图。

As for what's 'commonly' done, I don't think the problem has been around for long enough for there to be a commonly accepted method of handling the changeover. 至于“通常”做的事情,我认为问题存在的时间不足以至于没有一种普遍接受的处理转换方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM