C ++返回对象的引用 [英] C++ Returning Reference to New Object

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

问题描述

我知道有类似的问题,但没有一个给我一个确定的答案我的问题...



这些都是最好的做法吗?或者我应该返回一个指针?
如果不是应该如何改变以遵循最佳实践。



我想从函数返回一个新对象的引用。我的实现如下:

  MyClass& doSomething(){
return *(new MyClass());
}

MyClass a = doSomething();这是正常的,因为MyClass的一个新实例正在堆上被分配新的

>

或者我应该让它保持不变(我不知道什么时候做这个或不是)?

  const MyClass& doSomething(){
return {(new MyClass());
}

如果这两个都是错误的,我应该返回一个指向新的对象?



谢谢。

解决方案

无效,这不是一个好主意。

  MyClass& doSomething(){
return *(new MyClass());
}

返回后,没有人有原始指针, c $ c> delete it。*所以这是一个内存泄漏。



你应该几乎不会写一个 new ,除非您有相应的 delete - 或者更好的智能指针构造函数。






同时,原始代码中的这一行:

  MyClass a = doSomething ); 

...将要创建值的副本。假设不是另一个必须修复的错误,为什么要分配一个对象并返回一个引用复制和泄漏?只需返回值的对象:

  MyClass doSomething(){
return MyClass
}

现在您不必担心删除任何内容,任何在堆上。






最佳实践通常可以在四个字母RAII:资源获取初始化。 (和推论,那破坏是释放。)如果你有一些是不可能的或昂贵的,通过价值传递,然后传递一些句柄的价值。例如:

  unique_ptr< MyClass> doSomething(){
return unique_ptr< MyClass>(new myClass());
}

unique_ptr< MyClass> a = doSomething();

现在它只是一个指针被复制。对象本身在 doSomething 中创建,并且在 a 超出范围时删除



另一方面,如果 MyClass






*

这不是不可能永远删除它;你总是可以使用一个指针的引用和 delete 。这是不太可能你会这样做,它会看起来尴尬。如果你想传递指针,传递指针。



**通过easily-copyable我的意思是,很容易安全地复制它们,而且你实际上是这样做的。例如,原始指针或文件句柄只是几个字节,默认的复制构造函数会很乐意为你复制它们...但是最后你会得到对同一个堆对象或文件的多个引用,这是不可能的谁负责删除或关闭它。


I know there are similar questions to this but none of them give a definitive answer to my question...

Are both these okay in terms of best practices? Or should I be returning a pointer? And if not how should they be changed to follow best practices.

I want to return a reference to a new object from a function. My implementation is as follow:

MyClass& doSomething() {
   return *(new MyClass());
}

MyClass a = doSomething();

Is this okay because a new instance of MyClass is being allocated on the heap with new?

Or should I be making it constant (I'm not really sure when to do this or not)?

const MyClass& doSomething() {
    return *(new MyClass());
}

And if both these are wrong should I just be returning a pointer to the new object?

Thanks.

解决方案

While this isn't exactly invalid, it's not a good idea.

MyClass& doSomething() {
   return *(new MyClass());
}

After you return, nobody has the original pointer, so nobody will ever delete it.* So it's a memory leak.

You should pretty much never write a new unless you have a corresponding delete—or, better, a smart pointer constructor.


Meanwhile, this line in your original code:

MyClass a = doSomething();

… is going to make a copy of the value anyway. Assuming that wasn't another bug that has to be fixed, why bother heap-allocating an object and returning a reference to copy and leak? Just return the object by value:

MyClass doSomething() {
   return MyClass();
}

Now you don't have to worry about deleting anything, because you never created anything on the heap.


Best practices can usually be summed up in the four letters RAII: Resource Acquisition Is Initialization. (And the corollary, that destruction is release.) If you have something that's impossible, or expensive, to pass around by value, then pass around some handle to it by value. For example:

unique_ptr<MyClass> doSomething() {
    return unique_ptr<MyClass>(new myClass());
}

unique_ptr<MyClass> a = doSomething();

Now it's just a pointer being copied around. The object itself gets created inside doSomething, and deleted whenever a goes out of scope (or, if you pass it along to another variable, whenever that goes out of scope, etc.).

On the other hand, if MyClass is just a handful of easily-copyable values**, just copy it.


* It's not impossible to ever delete it; you can always take a pointer to the reference and delete that. It's just very unlikely you will ever do so, and it will look awkward. If you want to pass pointers around, pass pointers around. If you don't want to pass pointers around, wrap ownership up in a class and pass the class around by value.

** By "easily-copyable" I mean that it's easy to copy them safely, and that you actually do so. For example, a raw pointer or a file handle is just a few bytes, and the default copy constructor will gladly copy them over for you… but then you end up with multiple references to the same heap object or file, and it's impossible to track who's in charge of deleting or closing it.

这篇关于C ++返回对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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