从方法返回对象的最佳方法是什么? [英] what is the best way to return an object from a method?

查看:139
本文介绍了从方法返回对象的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我希望从c ++中的方法返回一个对象,



如果我按以下方式在堆栈上分配对象:



Hi all,

I wish to return an object from a method in c++,

If I allocate the object on the stack in the following way:

class A
{

  B GetObject()
  {
    B b(5);

    return b;
  }

};

public void main()
{
  A a;
  B b = a.GetObject();
}





b对象的析构函数之前是否总是会调用b的复制构造函数(在main中)在堆栈上定义?



这是从C ++中的方法传递对象的首选方法吗?



我不想在堆上分配对象,因为不清楚谁负责删除它。



提前谢谢,

Anton



Will the copy constructor of b (in the main) will always be called before the destructor of the B object defined on the stack?

Is this the preferable way to pass objects from methods in C++?

I don't want to allocate the object on the heap since it won't be clear who is responsible for deleting it.

Thanks in advance,
Anton

推荐答案

如果B类相对较小,您展示的方法是可行的。按值返回对象意味着必须将对象复制到堆栈上的临时对象(通过其复制构造函数),然后必须将其复制到调用程序中的b对象(再次通过其复制构造函数) ,最后堆栈上的临时对象将被销毁。不可否认,现代编译器可以优化大部分开销。但剩下的是至少一次调用复制构造函数。



另一种方法是在堆上创建对象并返回一个指针。正如您已正确指出的那样,这使得使用调用者删除对象的责任,这有点令人不愉快,即使您在函数的文档中强调这一点。它还有另一个缺点:从堆中分配空间通常比从堆栈分配更耗时。对于更大的对象,这是要走的路,并且堆开销相对较小。



将责任删除给调用者的对象的缺点是通过返回对象的自动指针可以避免肩膀。这样调用者就不会忘记删除它。



第三种方法是由调用者在堆栈上分配对象并传输对它的引用。然后,您的函数负责初始化对象,但所有权仍由调用者保留。这通常是禁食方法,但不能总是使用,因为有些类在构造函数中需要一些参数,现在你必须在调用函数中满足 - 而不是你真正想要的。要使用这种方法,必须将类设计为以这种方式初始化。



因此,没有适合所有方法的完美方法。在像C#这样的语言中,一切看起来都容易得多。但是再一次,在C#中你总是会在堆上创建对象(值类型对象除外),因此总是会导致使用堆的性能损失。
The method you have shown is viable if class B is relatively small. Returning the object by value means that the object will have to be copied to a temporary object on the stack (via its copy constructor), then it will have to be copied to the b object in the calling program (again via its copy constructor), and finally the temporary object on the stack will be destroyed. Admittedly, modern compilers can optimize much of this overhead away. But what remains is at least one call to the copy constructor.

The alternative is to create the object on the heap and return a pointer. As you have pointed out correctly, this leaves the responsibility of deleting the object with the caller, which is somewhat unpleasant, even if you emphasize this in the documentation of your function. It has also another disadvantage: Allocating space from the heap is generally more time consuming than allocating from the stack. For bigger objects this is however the way to go, and the heap overhead is comparatively small.

The disadvantage of putting the responsibility for the deletion of the object on the caller's shoulder can be avoided by returning an auto-pointer to the object. That way the caller cannot forget to delete it.

The third method would be to allocate the object on the stack by the caller and transfer a reference to it. Your function is then responsible for initializing the object, but ownership remains with the caller. This is generally the fasted method, but cannot always be used, because some classes require already some arguments in their constructor, which you now have to satisfy in the calling function -- not what you actually wanted. To use this method the class must be designed to be initialized in that way.

So, there is no perfect method that fits all. In languages like C# everything looks so much easier. But then again, in C# you would always create the object on the heap (except for value-type objects) and hence incur always the performance penalty of using the heap.


你的代码有问题。因为它超出范围你会返回一个被破坏的对象。



我更喜欢两个解决方案

your code is problematic. Because it is going out of scope ou return an destroyed object.

I prefer two solution
B* GetObject()
{
 B *b = new B(5);

return b; 
}
//My preferred way is "pass per reference" the object but do work

bool FillObject(B& b)
{
 b.value = 5;

 return true;//real error checking
}

//call
FillObject(theObject);





为什么不你的写作



why dont your write only

B *b = new B(5);



???


???


这篇关于从方法返回对象的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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