返回一个指针,而不是传递一个对象的引用来存储答案在C + + [英] Returning a pointer vs. passing a reference to an object to store the answer in C++

查看:118
本文介绍了返回一个指针,而不是传递一个对象的引用来存储答案在C + +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个常用的问题,就是在这个特定场景中使用指针和引用。将值存储在对象中以供调用者稍后使用。我可以使用指针或引用。



虽然,我宁愿使用引用,因为我尽量避免指针,有任何利弊



使用指针的代码如下:

  Node *& computeNode(){
//在创建节点对象之前进行一些计算。
Node * newNode = new Node;
newNode-> member1 = xyz;
newNode-> member2 = abc;
//等等...
return newNode;
}

使用引用的代码可以执行这样的操作:

  void computeNode(Node& newNode){
//在为节点对象赋值之前进行一些计算。
newNode.member1 = xyz;
newNode.member2 = abc;
//等等。
}

我可以看到的差异如下:


  1. 当使用指针方法时,newNode对象在堆上分配。所以,除非我调用删除它,它不会被删除。然而,在引用方法中,是否在Heap / Stack上分配newNode取决于调用者在创建newNode对象时所做的。


  2. ,传递给该函数所需的参数数量至少增加1.这很好,只有我觉得将返回对象传递给函数调用还有点反直觉,除非我以这样的方式命名函数:


  3. 通过使用引用,我可以模拟多个对象的返回。在指针方法中,我想我必须将所有对象包装在另一个结构中(如对类),然后返回它。这增加了开销。


但是,我不知道通常一个是优先于另一个。如果在C ++中有任何函数命名约定,让开发人员知道他应该将返回对象也作为参数传递。

解决方案

第二种方法可能是更好的,因为如果忘记了 delete 返回的指针,则不会发生内存泄漏。



这通常是一个好的做法,以这样一种方式,每个分配堆内存的函数或对象也释放该内存。你的第一个例子违反了这种做法,使它成为函数调用者释放内存的责任。这使得内存泄漏更有可能,因为现在每次调用函数时都有另一个机会忘记删除返回的指针。



您可能还需要考虑返回在对象的大小不是那么大的情况下,按对象的值(它将返回对象的副本)。即使这将需要创建一个副本,如果对象不是这么大,它不会影响性能。 (这种方法将在未来变得更有吸引力,与C ++ 0x移动语义。)


I have general question regarding the use of pointers vs. references in this particular scenario.

Let's say that I have a function that is going to do some computation and store the value inside an object for later use by the caller. I can implement this by using either pointers or references.

Although, I would prefer using references because I trying avoiding pointers as much as possible, are there any pros/cons of one approach over the other.

The code using Pointers would be as follows:

Node*& computeNode() {  
  // Do some computation before creating a node object.  
  Node* newNode = new Node;  
  newNode->member1 = xyz;  
  newNode->member2 = abc;  
  // and so on ...  
  return newNode;  
}

The code using references could do something like this:

void computeNode(Node& newNode) {  
   // Do some computation before assigning values to the node object.  
   newNode.member1 = xyz;  
   newNode.member2 = abc;  
   // and so on.  
}

The differences that I can see are as follows:

  1. When using the pointer method, the newNode object is allocated on the Heap. So, unless I call delete on it, it is not going to get deleted. However, in the reference method, whether newNode is allocated on the Heap/Stack depends on what the caller did to create the newNode object.

  2. Whenever we use references, the number of arguments needed to pass to the function increases by at least 1. This is fine, only I find it a bit counter-intuitive to pass the return object also to a function call unless I name the function in such a way that it becomes obvious to the API user.

  3. By using references, I can simulate the return of multiple objects. In the pointer method, I think I will have to wrap all the objects in another structure (like a pair class) and then return it. That increases the overhead.

However, I do not know if usually one is preferred over the other. And if there are any function naming conventions in C++ that let the developer know that he is supposed to pass the return object also as an argument.

解决方案

The second approach is probably preferable because there is no possibility of a memory leak, in the event you forget to delete the returned pointer.

It's usually good practice to code in such a way that each function or object which allocates heap memory also deallocates that memory. Your first example violates that practice, making it the function caller's responsibility to deallocate the memory. This makes memory leaks more likely, because now every time the function is called there is another opportunity to forget to delete the returned pointer.

You may also want to consider returning the object by value (which will return a copy of the object) in cases where the size of the object is not that large. Even though this will require a copy to be created, if the object is not so large it won't impact performance. (This method will become a lot more attractive in the future with C++0x move semantics.)

这篇关于返回一个指针,而不是传递一个对象的引用来存储答案在C + +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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