简体   繁体   English

返回类型在C ++中选择

[英]return type choose in C++

In C, I think there is no reference, so the return value could be a pointer or the datatype itself. 在C语言中,我认为没有引用,因此返回值可以是指针或数据类型本身。 If it's a local variable, return a pointer doesn't make any sense. 如果它是局部变量,则返回指针没有任何意义。 If the variable is dynamically allocated, only pointer can be returned. 如果变量是动态分配的,则只能返回指针。

However, in C++, there is a new choice, reference. 但是,在C ++中,有一个新的选择,即引用。 If what I want to return is a local variable, I think I can only choose to return itself, because the other two will have nothing to refer or point to after the function return. 如果我要返回的是局部变量,我想我只能选择返回自身,因为其他两个在函数返回之后将没有其他要引用或指向的地方。 If I dynamically allocate a variable, what do I return? 如果我动态分配变量,该返回什么? Pointer or Reference? 指针还是参考? What's the advantage and disadvantage? 优点和缺点是什么? Why don't just return the variable? 为什么不只返回变量?

A very simple example: 一个非常简单的例子:

class TreeNode
{
//......May be some elements and functions.
}

TreeNode& test()
{
  TreeNode* temp = new TreeNode;
  return *temp;
}

I don't what to return, pointer or reference? 我没有返回什么,指针或参考?

If I dynamically allocate a variable, what do I return? 如果我动态分配变量,该返回什么?

Return a pointer (a regular one or a smart one, depending on a situation). 返回一个指针(根据情况返回常规指针或智能指针)。 Do not return a reference to dynamically allocated objects: eventually, you will need to release the memory for the object; 不要返回对动态分配对象的引用:最终,您将需要释放该对象的内存; the construct delete &someRef that you would have to use is extremely counterintuitive. 您将不得不使用的构造delete &someRef非常违反直觉。

Returning a reference is appropriate when you are returning from a member function, and a reference that you are returning is to a member, or when you are returning a reference to an object that has been passed to your function in the first place. 当从成员函数返回时,返回的引用是适当的,而返回的引用是对成员的引用,或者当您返回对首先传递给函数的对象的引用时,返回引用是合适的。

Why don't just return the variable? 为什么不只返回变量?

This is a very valid choice as well: returning by value lets you not worry about memory management and object ownership. 这也是一个非常有效的选择:按值返回使您不必担心内存管理和对象所有权。 The biggest obstacle there is the cost of copying. 最大的障碍是复制成本。 However, their inefficiencies are are often grossly overestimated, leading to premature optimization. 但是,它们的效率低下常常被严重高估,导致过早的优化。

In the final compiled machine code a reference is basically the same as a pointer. 在最终的编译后的机器代码中,引用与指针基本上相同。 Its just C++ that defines a different set of rules and syntax for references. 它只是C ++,为引用定义了一组不同的规则和语法。 Think of it as a pointer that can be (and must be) initialized only once when you declare it and it can never be NULL . 将其视为一个指针,在声明它时只能(必须必须)初始化一次,并且永远不能为NULL If you return a reference then you are basically returning a pointer but you have signaled to the caller that the return value is a "pointer" (reference) that can not be NULL . 如果返回引用,则基本上是在返回一个指针,但已向调用者发出信号,表明返回值是不能为NULL的“指针”(引用)。 Reference parameters are similar, with a reference function/method parameter you are just passing a pointer to something and you tell the function implementation that this "pointer" is never NULL . 引用参数与之类似,使用引用函数/方法参数时,您只是将指针传递给某物,并且您告诉函数实现此“指针”从不为NULL

If you dynamically allocate a variable, you can return either the pointer or the reference. 如果动态分配变量,则可以返回指针或引用。

If the variable you are returning consumes large amount of memory, it is better to return the pointer or the reference than the variable itself. 如果要返回的变量消耗大量内存,则返回指针或引用比变量本身更好。

If you return a reference or a pointer then you return the address of the variable. 如果返回引用或指针,则返回变量的地址。

If you return the variable then you return a copy of the variable. 如果返回变量,则返回变量的副本。 With big variables there is the overhead for the copy (especially when dynamically allocated, with local variables the compiler can in some circumstances optimize the code and avoid the copy). 对于大变量,存在复制的开销(尤其是在动态分配时,对于局部变量,编译器在某些情况下可以优化代码并避免复制)。

As already said before, the difference between reference and pointer is in the syntax and rules. 如前所述,引用和指针之间的区别在于语法和规则。 Basically choose which one you like most. 基本上选择最喜欢的那个。 Pointers are more explicit. 指针更加明确。 With pointers you may remember that you have to deallocate a dynamic variable. 使用指针,您可能还记得必须取消分配动态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM