C ++ function's return value - & returns a reference type, the non-reference type

Function's return is divided into the following situations:

1, the main function return values:

Allowing the main function does not return a value can be ended; main function return value can be regarded as a status indicator, return 0 indicates that the program runs successfully, most of the other return value indicates failure.

2, the return of non reference types:

  • Function's return value is used to initialize the temporary objects created in the function call (temporary object), if the return type is not referenced in the calling function places will copy function return value to a temporary object.
  • When evaluating an expression, and if you need a place to store the calculation result, the compiler will not create an object name, which is a temporary object. C ++ programmers usually temporary term to replace a temporary Object.
  • Initializes the value of a function returns a temporary object and method for initialization parameter argument is the same.
  • When the function returns a non-reference type, the return value may be a local object, may also be the result of evaluating an expression.

3, returns a reference type:

  • When the function returns a reference type, the return value is not copied, the contrary, returns the object itself.
  • Do not return a reference to a local object! Do not return a pointer to a local object pointer!

  When the function is finished, release the memory space allocated to a local object. In this case a reference to a local object will point to the uncertainty of memory! Returns a pointer pointing to a local object is the same, when the function terminates, the local object is released, the returned pointer becomes longer exists depending pointer object.

  • When returns a reference, it is required function parameters, with the parameters that need to be returned in a reference or pointer present embodiment.

4, return const type:

  The return value directly to the lifetime of a variable is not over yet, and therefore, the return value of a function (or called function result) any operation itself, in fact, is the operation of the variable, which is the introduction of const type return of meaning. When using the const keyword, which means that the return value of a function can not be modified immediately! The following code, the compiler can not, because this is the return value of the operation ++ (corresponding to the variable z operation performed ++) immediately, for which the function is not allowed. If const removed, re-compilation, it can be obtained by forming and printing the result z = 7.

include <iostream>
include <cstdlib>
const int& abc(int a, int b, int c, int& result)
{
    result = a + b + c;
    return result;
}
int main()
{
   int a = 1; int b = 2; int c=3;
   int z;
   abc(a, b, c, z)++;  //wrong: returning a const reference
   std::cout << "z= " << z << std::endl;
   return 0;
}

 

Guess you like

Origin www.cnblogs.com/mathyk/p/11527502.html