从 C++ 返回对象的正确方法是什么?功能?

发布于 2024-09-28 00:47:23 字数 296 浏览 2 评论 0原文

我很困惑:

  • 返回一个对象(但随后该对象是从函数中的局部变量复制的,这会消耗内存)
  • 返回一个指针(但随后你必须记得在调用代码中删除它,这很奇怪)
  • 返回一个引用(但这不可能,因为这将是对函数的局部变量的引用,该变量将立即被删除当函数结束时)

我正在寻找从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

挽心 2024-10-05 00:47:23

现代编译器通常实现(命名)返回值优化,您引用的副本(并且逻辑上期望的)尚未完成。

从 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.

榕城若虚 2024-10-05 00:47:23

中的 std::auto_ptr 怎么样?或者如果涉及 C++0x std::unique_ptr

What about std::auto_ptr from <memory>? Or if C++0x is concerned std::unique_ptr?

疧_╮線 2024-10-05 00:47:23

关于从函数返回对象的一些经验法则:

返回每个副本,除非

  1. 您返回非本地对象(如类成员、静态变量等)的类型,您将根据 const 引用传递给函数;您可以返回此每个const引用
  2. 您返回非本地对象并且调用者应该能够调用返回对象的修改成员,从而操作存储在其他地方的对象;返回此每个非const引用
  3. 您返回多态类层次结构中的派生类,对象的用户应该只知道基类,#1 和 #2 都不适用;返回此每个智能指针

A few rules of thumb regarding returning objects from functions:

Return per copy, except when

  1. you return a non-local object (like a class member, static variable etc.) of a type that you would pass to a function per const reference; you can return this per const reference
  2. you return a non-local object and callers should be able to invoke modifying members of the returned object, thereby manipulating an object stored elsewhere; return this per non-const reference
  3. you return a derived class in a polymorphic class hierarchy, users of the object should only know the base class, and neither #1 nor #2 apply; return this per smart pointer
小苏打饼 2024-10-05 00:47:23

假设“没有库处理指针并自动释放内存”意味着没有指针返回,没有 boost::shared_ptr 和 std::unique_ptr (std: 都是邪恶),你有两种选择:

:auto_ptr无论如何 值:

Foo bar(quux)
{
    Foo foo;
    foo.frobnicate(quux);
    return foo;
}

Foo foo = bar("fred");

通过引用传递:

void bar(Foo& foo, quux)
{
    foo.frobnicate(quux);
}

Foo foo;
bar(foo, "fred");

Assuming "no library handling pointers and freeing memory automatically" means no return-by-pointer, no boost::shared_ptr and no std::unique_ptr (std::auto_ptr is evil anyway), you have two choices:

Return by value:

Foo bar(quux)
{
    Foo foo;
    foo.frobnicate(quux);
    return foo;
}

Foo foo = bar("fred");

Pass by reference:

void bar(Foo& foo, quux)
{
    foo.frobnicate(quux);
}

Foo foo;
bar(foo, "fred");
﹂绝世的画 2024-10-05 00:47:23

取决于对象的“语义”。值应该通过复制返回,而实体应该(或必须,因为理想情况下它们是不可复制的)由指针或引用返回。

应尽可能使用参考文献。但是,如果您必须返回一个指针,那么使用智能指针类(例如 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.

兮子 2024-10-05 00:47:23

按值返回,除非您需要子类型多态性。在后一种情况下,我将返回 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 a unique_ptr<T> (C++0x).

倾`听者〃 2024-10-05 00:47:23
  • 返回智能指针选项之一(要么必须依赖额外的库,要么等到 C++1x)
  • 使用引用传递

我个人更喜欢第二个选项,因为很明显用户需要分配和删除内存。

  • Return one of the smart pointer choices (either having to depends on extra libraries or wait until C++1x)
  • Use pass by reference

Personally I prefer the second option because it is clear that the user needs to allocate and delete memory.

风情万种。 2024-10-05 00:47:23

返回一个对象(但随后
对象是从本地复制的
函数中的变量,其中
消耗内存)

最佳编译器可能不会花费大量时间来创建副本。
您可能还需要实现复制构造函数和重载赋值运算符,具体取决于您的对象内容。

返回一个指针(但是你有
记得删除它,在
调用代码,这很奇怪)

是的,你必须记住删除它,因为你不想考虑这个问题的自动清理。

返回一个引用(但这不是
可能的,因为这将是
对局部变量的引用
函数,它将被删除为
函数结束后立即)

您从成员函数返回 this 对象(*this) 时,返回引用非常有用。否则,就像你提到的那样,它无法使用。

总体而言:这取决于您的需要,如上所述,何时选择哪一个。

returning an object (but then the
object is copied from the local
variable in the function, which
consumes memory)

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.

returning a pointer (but then you have
to remember to delete it, in the
calling code, which is weird)

Yes, you have to remember to delete it as you do not want to consider automatic cleanup for this question.

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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文