Help réferences fonctionf()=x;

Je voudrais connaitre le cheminement point par point de ce programme,en praticulier le process int &alterne. Merci d'avance pour votre aide.
#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;}
}
Effectively, that is same as this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
  int n=1, p=3, q=5;
  n = 0;
  cout << "n=" << n << "_ p=" << p << "\n";
  q = 12;
  cout << "p=" << p << "_q=" << q << "\n";
}


Do you know how static variables behave?
1
2
3
4
5
6
bool ticktock()
{
  static bool bar = false;
  bar = !bar;
  return bar;
}


A reference to reference:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
  int foo = 0;
  int& bar = foo;
  int& gaz = bar;
  gaz = 42;
  std::cout << foo << '\n';
}

Thank you very much keskiverto
Topic archived. No new replies allowed.