如何实现一个C ++方法创建一个新的对象,并返回一个引用 [英] How to implement a C++ method that creates a new object, and returns a reference to it

查看:216
本文介绍了如何实现一个C ++方法创建一个新的对象,并返回一个引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ AuthenticatingProxy类实例与下面的方法。此方法创建一个Response对象,然后使用状态更新,然后返回。因为Response对象的内部,它不能被复制(即我不能简单地返回值)。

I have a C++ AuthenticatingProxy class instance with the below method. This method creates a Response object, which is then updated with state, and then returned. Because of the internals of the Response object it cannot be copied (i.e. I cannot simply return by value).

const Response& AuthenticatingProxy::Get(const std::string& host,
                                  const std::string& path,
                                  const http_headers& headers)
{
  static Response response;
  // do the HTTP call, and set response's state here
  return response;
}

AuthenticatingProxy类中的此方法上可能发生许多调用,变量在这里是不理想的。有人可以请建议一个解决方案,返回一个引用,不会被销毁的功能退出?非常感谢。

Many calls can occur on this method in the AuthenticatingProxy class, so having a static variable in here is not ideal. Could someone please suggest a solution that returns a reference that is not destroyed on the function exiting? Thanks.

我在研究中最近的是最好的做法页面,提到通过引用返回,而不是值,创建一个大对象 m还没找到例子!所有的例子都是用于传入并返回的引用,或者对于int&样式引用 - 而不是在函数本身内创建对象实例的那些。

The nearest I've got in my research are best practices pages that mention "Return by reference, not value, where a large object is created" but I'm yet to find an example! All the examples are for references that are passed in and then returned, or for int& style references - not ones where the object instances are created within the function itself.

我认为解决方案可能存在将函数返回响应作为值,但在调用代码使用const变量来捕获它。不知道如果这只是工作为int虽然 - 所有的例子我发现使用基本类型。任何帮助非常感谢。

I think the solution may exist in having the function return response as a value, but in the calling code using a const variable to capture it. Not sure if that only works for int though - all examples I find use basic types. Any help greatly appreciated.

推荐答案


由于Response对象的内部,即我不能简单地返回值)。

Because of the internals of the Response object it cannot be copied (i.e. I cannot simply return by value).

你正在描述一个存在于C ++ 03中但不再存在的问题

You are describing a problem that existed in C++03, but which no longer exists.

以旧的爱情为例 std :: fstream
基本上,它拥有用于从/向文件读/写的文件描述符/句柄。

由于C ++的性质和设计, fstream

Take for example the old loved std::fstream.
Basically, deep inside*, it held a file descriptor/handle that is used to read/write from/to the file.
Because of the nature and design of C++, the fstream destructor closed that file handle in order to clean up the object and prevent handle leaks.

由于 fstream

Because of the internals of the fstream object, it could not be returned by value. Returning it by value meant to somehow prevent the destructor from closing that file handle, or to make the copy constructor duplicate that handle in some sort. Having cross platform solution is extremly difficult, if not impossible. Not to mention that copying the internal buffer is just plain wrong.

因此,直到C ++ 11,你不能返回 fstream 按值。

然后,发明了移动语义

So up until C++11, you couldn't return fstream by value.
Then move semantics was invented.

在C ++ 11中,您只需移动对象,而不是复制它。在 fstream 示例中,move构造函数shallow复制文件句柄,同时使原始文件句柄指针无效。原始对象析构函数将检查原始文件句柄,看到它是无效的,并跳过关闭它。

In C++11, you can just move the object instead of copying it. In the fstream example, the move constructor shallow copies the file handle, while invalidating the original file-handle pointer. The original object destructor would inspect the original file-handle, see that it is invalid and skip closing it.

这是您的问题的惯用解决方案。虽然你不能复制对象,你可以明确地实现移动语义。对象的内部将被移动到返回值,而原始对象仍然是某种类型的空。

This is the idiomatic solution for your problem. While you cannot copy the object, you can definitely implement move semantics for it. The internals of the object will be moved to the return value while the original object remains "empty" in some sort.

在这个SO中可以找到移动语义的很好的解释answer:什么是移动语义?

Good explanations of move semantics can be found in this SO answer: What are move semantics?

如果这个解决方案不可能,在动态内存存储(堆)中声明对象,并通过一些智能指针返回它。

If that solution is not possible as well, declare the object in the dynamic memory storage (the "heap") and return it by some smart pointer.


如何实现创建一个新对象的C ++方法,并返回一个
引用

How to implement a C++ method that creates a new object, and returns a reference to it

即使可以,也不要这样做。

Don't ever do it even if you can.

*是的,文件句柄可以被存储在 streambuff 对象中,我简单地描述这个问题。

*yes, the file handle can be stored in the streambuff object instead, I'm describing the problem in a nutshell.

这篇关于如何实现一个C ++方法创建一个新的对象,并返回一个引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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