[Turn] C / C ++ reference to the return value as a function of

Transfer: https://blog.csdn.net/weixin_40539125/article/details/81410008

This article is very well written:

语法:类型 &函数名(形参列表){ 函数体 }

pay attention:

1. cited as the function return value, the function name must be defined when the function &

2. The biggest advantage cited as the return value of the function is not to produce a copy of the return value in memory

  1.  
    // Code Source: RUNOOB
  2.  
    #include<iostream>
  3.  
    using namespace std;
  4.  
    float temp;
  5.  
    float fn1(float r){
  6.  
    temp=r*r* 3.14;
  7.  
    return temp;
  8.  
    }
  9.  
    & a float Fn2 ( a float R & lt) { // return a reference to & temp described, in other words to return itself temp
  10.  
    temp=r*r* 3.14;
  11.  
    return temp;
  12.  
    }
  13.  
    int main () {
  14.  
    A = Fn1 a float ( 5.0); // Case. 1: Return Value
  15.  
    // float & b = fn1 (5.0); // case 2: return value as a function of the initialization value of reference [Error] invalid initialization of non-const reference of type 'float &' from an rvalue of type 'float'
  16.  
    // (Some compilers can successfully compile the statement, but will give a warning)
  17.  
    C = Fn2 a float ( 5.0); // Case. 3: return a reference
  18.  
    D = & Fn2 a float ( 5.0); // Case. 4: reference returned by the function as the initialization value of the new reference
  19.  
    cout<<a<<endl;//78.5
  20.  
    //cout<<b<<endl;//78.5
  21.  
    cout<<c<<endl;//78.5
  22.  
    cout<<d<<endl;//78.5
  23.  
    return 0;
  24.  
    }

case 1: Call the function with the return value (as shown below, Source: Bole Online):

When the return value of a global variable temp, C ++ creates a temporary variable and temp in-memory copy of the value to the temporary variable. After the return to the main function main, assignment a = fn1 (5.0) will then copy the value of the temporary variable to the variable a

case 2: the return value of a function initialize referenced calls the function (as shown below, Source: Bole Online)

In this case, the function Fn1 () value is returned to the mode, returns to the first copy of the value of the temporary variable temp. After returning to the main function, temporary variable initialized with the reference variable b, so that b be the temporary variable to the alias. Because of the scope of short-term temporary variables (in the C ++ standard, the life cycle of temporary variables or objects came to an end after the end of a complete statement expression, that is, statements float & b = fn1 (5.0); after), so b invalid facing danger, the value is likely to be a future value can not be determined.

 If you really want to use the return value of the function to initialize a reference, should first create a variable, the function's return value is assigned to the variable, and then use that variable to initialize reference:

  1.  
    int x=fn1(5.0);
  2.  
    int &b=x;

 case 3: call the function returns a reference by the way (as shown below, Source: Bole Online)

In this case, the function fn2 () return value does not produce a copy, but directly to the variable temp return to the main function, namely the left main function of the value assignment is copied directly from the variable temp from (that is to say temp variable c is just a copy rather than an alias), thus avoiding the generation of temporary variables. In particular, when the variable temp is a user-defined object class, this process also avoids the call class copy constructor creates temporary objects in memory, more efficient use of time and space program.

case 4: return with function references as a way to initialize a new reference value to the calling function (as shown below, Source: Bole Online)

In this case, the function Fn2 () copies the return value is not generated, but directly returned to the main function for variable temp. In the main function, with a reference to the return value declared d initialization, that is to say at this time becomes d alias variable temp. Because temp is a global variable, so in the life of a temp d remains active, so this approach is safe.

3. can not return a reference to a local variable. As in the example above, if temp is a local variable, then it will be destroyed after the function returns, at this time will become a reference temp "no meaning" references the program into an unknown state.

4. referenced memory can not return through the internal functions of the new allocation. Although there is no local variable passive destruction, but if the reference is returned by the function only as a temporary variable appearance, but did not assign it to a real variable, then it may cause space that reference points (with new assignment ) case can not be released (the absence of specific variable name, it can not release this memory with delete manually), resulting in a memory leak. So we should avoid this from happening

When 5 returns the class referenced by members, preferably const reference. This prevents damage to members of the class in the case of unintentional.

6. You can use the function reference lvalue of an assignment expression returned

  1.  
    #include<iostream>
  2.  
    using namespace std;
  3.  
    int value[10];
  4.  
    int error=-1;
  5.  
    int &func(int n){
  6.  
    if(n>=0&&n<=9)
  7.  
    return value[n];//返回的引用所绑定的变量一定是全局变量,不能是函数中定义的局部变量
  8.  
    else
  9.  
    return error;
  10.  
    }
  11.  
     
  12.  
    int main(){
  13.  
    func( 0)=10;
  14.  
    func( 4)=12;
  15.  
    cout<<value[0]<<endl;
  16.  
    cout<<value[4]<<endl;
  17.  
    return 0;
  18.  
    }

D.用引用实现多态

在C++中,引用是除了指针外另一个可以产生多态效果的手段。也就是说一个基类的引用可以用来绑定其派生类的实例

  1.  
    class Father;//基类(父类)
  2.  
    class Son:public Father{.....}//Son是Father的派生类
  3.  
    Son son; //son是类Son的一个实例
  4.  
    Father &ptr=son; //用派生类的对象初始化基类对象的使用

特别注意:

ptr只能用来访问派生类对象中从基类继承下来的成员。如果基类(类Father)中定义的有虚函数,那么就可以通过在派生类(类Son)中重写这个虚函数来实现类的多态。

 

Guess you like

Origin www.cnblogs.com/do-your-best/p/11229594.html