简体   繁体   中英

transmit a value to return by reference in C++

I have a function that receives two references of integer. A boolean indicator allows to choose one time the first reference and one time the second reference as it is illustrated in the code below :

1) I don't understand the meaning of alterne(n,p) = 0 and alterne(n,p) = 12

2) and the function of the static bool indic;

#include <iostream>
using namespace std;

int & alterne (int &, int &);

int main()
{
    int n=1, p=3, q=5;

    alterne(n,p) = 0;
    cout << "n=" << n << "p=" << p << "\n" ;

    alterne(p,q) = 12;
    cout << "p=" << p << "q=" << q << "\n" ;
}

int & alterne (int & n1, int & n2)
{
    static bool indic = true;
    if (indic) { indic =false; return n1; }
    else       { indic = true; return n2 ; }
}

thank you for your consideration

Does the following make sense to you? It's a function which takes an int by reference and assigns a new value to it.

void foo(int& x)
{
    x = 33;
}

1) I don't understand the meaning of alterne(n,p) = 0 and alterne(n,p) = 12

For your question, alterne returns int& . So you basically have the same situation as above (ie, a reference to an int is available and ultimately a new value is assigned to the referenced int ).

alterne(n, p) = 0;
// first call is equivalent to getting an `int&` to `n`
// then performing n = 0;

alterne(p, q) = 12;
// second call is equivalent to getting an `int&` to `q`
// then performing q = 12;

2) and the function of the static bool indic;

This is a variable local to the alterne function but has a lifetime such that the same bool is available in all calls to the function. It is initialized once and read during each function call. Therefore, it's initialized to true once which is available on the 1st function call and is toggled between false and true on each subsequent function call.

int & alterne (int & n1, int & n2)
{
    static bool indic = true;
    if (indic) { indic =false; return n1; }
    else       { indic = true; return n2 ; }
}

Lets break down the function:

static bool indic = true;

First there is a static variable inside a function body. Static here means that the life time of the variable is global, meaning it retains its value between function calls. The firs time you call alterne() it is ture .

    if (indic) { indic =false; return n1; }
    else       { indic = true; return n2 ; }

This basically toggles the variable indic . If it is ture it is false after. And since indic retains its value between function calls, the function alterne() basically alternates between returning n1 and n2 - starting with n1 .

int & alterne (int & n1, int & n2)

The function takes 2 references to ints and returns a reference to int . And as we saw above it actually once returns the reference to n1 and then n2 and so on.

 alterne(n,p) = 0

alterne(n,p) returns either n or p (based on the current value of indic variable and then assigns 0 to it. Since this is the first call to alterne it is the same as doing n = 0;

alterne(p,q) = 12;

Since this is the second call to alterne() it is the same as q = 12;

Hope this helps you in anyway.

alterne function is a switch that at first call return first argument by reference then in second time return second argument by reference .

Compiler after calling alterne function will replace left side of assign operator with return reference . then right side of assign operator will copy into it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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