如何“返回对象”在C ++? [英] How to "return an object" in C++?

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

问题描述

我知道标题听起来很熟悉,因为有很多类似的问题,但我要求的问题的不同方面(我知道在堆栈上的东西和把它们放在堆之间的区别)。

I know the title sounds familiar as there are many similar questions, but I'm asking for a different aspect of the problem (I know the difference between having things on the stack and putting them on the heap).

在Java中,我总是可以返回对本地对象的引用

In Java I can always return references to "local" objects

public Thing calculateThing() {
    Thing thing = new Thing();
    // do calculations and modify thing
    return thing;
}

在C ++中,做类似的事情我有两个选项

In C++, to do something similar I have 2 options

(1)我可以在需要返回对象时使用引用

(1) I can use references whenever I need to "return" an object

void calculateThing(Thing& thing) {
    // do calculations and modify thing
}

然后像这样使用

Thing thing;
calculateThing(thing);

或者我可以返回一个指向动态分配对象的指针

(2) Or I can return a pointer to a dynamically allocated object

Thing* calculateThing() {
    Thing* thing(new Thing());
    // do calculations and modify thing
    return thing;
}

然后使用

Thing* thing = calculateThing();
delete thing;

使用第一种方法我不需要手动释放内存,但对我来说,它使代码难以阅读。第二种方法的问题是,我必须记住 delete thing; ,这看起来不太好。我不想返回一个复制的值,因为它是低效的(我想),所以这里来的问题

Using the first approach I won't have to free memory manually, but to me it makes the code difficult to read. The problem with the second approach is, I'll have to remember to delete thing;, which doesn't look quite nice. I don't want to return a copied value because it's inefficient (I think), so here come the questions


  • 有第三个解决方案(不需要复制该值)

  • 如果我坚持第一个解决方案,有没有问题?

  • 使用第二个解决方案?

推荐答案


I don't want to return a copied value because it's inefficient

证明。

查看up RVO和NRVO,以及C ++ 0x移动语义。在大多数情况下,在C ++ 03中,out参数只是一个很好的方法来使你的代码丑陋,而在C ++ 0x你实际上是通过使用out参数伤害自己。

Look up RVO and NRVO, and in C++0x move-semantics. In most cases in C++03, an out parameter is just a good way to make your code ugly, and in C++0x you'd actually be hurting yourself by using an out parameter.

只需写干净的代码,返回值。如果性能是一个问题,配置它(停止猜测),并找到可以做什么来解决它。它可能不会从函数返回东西。

Just write clean code, return by value. If performance is a problem, profile it (stop guessing), and find what you can do to fix it. It likely won't be returning things from functions.

也就是说,如果你死了,你可能想要做out参数。它避免了动态内存分配,这是更安全,一般更快。它需要你有一些方法来构造对象之前调用函数,这并不总是对所有对象有意义。

That said, if you're dead set on writing like that, you'd probably want to do the out parameter. It avoids dynamic memory allocation, which is safer and generally faster. It does require you have some way to construct the object prior to calling the function, which doesn't always make sense for all objects.

如果你想使用动态分配,至少可以做的是把它放在一个智能指针。 (这应该一直做所有)然后你不用担心删除任何东西,事情是异常安全等等。唯一的问题是它可能比返回的值反之慢!

If you want to use dynamic allocation, the least that can be done is put it in a smart pointer. (This should be done all the time anyway) Then you don't worry about deleting anything, things are exception-safe, etc. The only problem is it's likely slower than returning by value anyway!

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

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