从 C++ 返回对象的正确方法是什么?功能?
我很困惑:
- 返回一个对象(但随后该对象是从函数中的局部变量复制的,这会消耗内存)
- 返回一个指针(但随后你必须记得在调用代码中删除它,这很奇怪)
- 返回一个引用(但这不可能,因为这将是对函数的局部变量的引用,该变量将立即被删除当函数结束时)
我正在寻找从 C++ 函数返回对象的正确方法,在基本 C++ 中(没有库处理指针并自动释放内存)。我想知道这应该如何完成。
谢谢
I am confused between :
- returning an object (but then the object is copied from the local variable in the function, which consumes memory)
- returning a pointer (but then you have to remember to delete it, in the calling code, which is weird)
- returning a reference (but this is not possible because this would be a reference to a local variable of the function, which would be deleted as soon as the function ends)
I am looking for the proper way to return an object from a C++ function, in basic C++ (no library handling pointers and freeing memory automatically). I want to know how this is supposed to be done.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
现代编译器通常实现(命名)返回值优化,您引用的副本(并且逻辑上期望的)尚未完成。
从 Visual Studio 2005 (VC++ 8.0) 开始,我就不再考虑返回对象。
Modern compilers typically implement the (Named) Return Value Optimization, by which the copy you reference (and would logically expect) is not done.
Ever since Visual Studio 2005 (VC++ 8.0) I don't think twice about returning objects.
中的std::auto_ptr
怎么样?或者如果涉及 C++0xstd::unique_ptr
?What about
std::auto_ptr
from<memory>
? Or if C++0x is concernedstd::unique_ptr
?关于从函数返回对象的一些经验法则:
返回每个副本,除非
const
引用传递给函数;您可以返回此每个const
引用,const
引用,A few rules of thumb regarding returning objects from functions:
Return per copy, except when
const
reference; you can return this perconst
referenceconst
reference假设“没有库处理指针并自动释放内存”意味着没有指针返回,没有 boost::shared_ptr 和 std::unique_ptr (std: 都是邪恶),你有两种选择:
:auto_ptr无论如何 值:
通过引用传递:
Assuming "no library handling pointers and freeing memory automatically" means no return-by-pointer, no
boost::shared_ptr
and nostd::unique_ptr
(std::auto_ptr
is evil anyway), you have two choices:Return by value:
Pass by reference:
取决于对象的“语义”。值应该通过复制返回,而实体应该(或必须,因为理想情况下它们是不可复制的)由指针或引用返回。
应尽可能使用参考文献。但是,如果您必须返回一个指针,那么使用智能指针类(例如 std::auto_ptr 或 boost::shared_ptr)是一个好主意,因为这样调用代码就不必考虑在使用完它后是否要释放它。
Depends on the "semantics" of the object. Values should be returned by copy while entities should (or must, since they are ideally not copyable) be returned by pointer or reference.
References should be used when possible. But if you must return a pointer, using a smart pointer class such as std::auto_ptr or boost::shared_ptr is a good idea, because then the calling code don't have to wonder about freeing it when it is done with it.
按值返回,除非您需要子类型多态性。在后一种情况下,我将返回
auto_ptr
(C++03) 或unique_ptr
(C++0x)。Return by value unless you need subtype polymorphism. In the latter case, I would return an
auto_ptr<T>
(C++03) or aunique_ptr<T>
(C++0x).我个人更喜欢第二个选项,因为很明显用户需要分配和删除内存。
Personally I prefer the second option because it is clear that the user needs to allocate and delete memory.
最佳编译器可能不会花费大量时间来创建副本。
您可能还需要实现复制构造函数和重载赋值运算符,具体取决于您的对象内容。
是的,你必须记住删除它,因为你不想考虑这个问题的自动清理。
您从成员函数返回 this 对象(*this) 时,返回引用非常有用。否则,就像你提到的那样,它无法使用。
总体而言:这取决于您的需要,如上所述,何时选择哪一个。
Optimal compilers may not take significant time to create copy.
You may also need to implement copy constructor and overload assignment operator, depending upon your object contents.
Yes, you have to remember to delete it as you do not want to consider automatic cleanup for this question.
Returning reference is useful when you are returning this object(*this) from member functions. Otherwise, like you mentioned its not possible to use.
Overall: it depends upon your need as described above regarding which one to choose when.