12

Ref

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. float &index( float *v, int i )
  6. {
  7.     return v[ i ];
  8. }
  9.  
  10. int main() {
  11.     int x = 33;
  12.     int &y = x;
  13.  
  14.  
  15.     cout << "x (0x" << &x << ") = " << x << endl;
  16.     cout << "y (0x" << &y << ") = " << y << endl;
  17.  
  18.     y = 99;
  19.  
  20.     cout << "y (0x" << &y << ") = " << y << endl;
  21.     cout << "x (0x" << &x << ") = " << x << endl;
  22.  
  23.     int *pi = &x;
  24.  
  25.     cout << "pi (0x" << &pi << ") = 0x" << pi << " = " << *pi << endl;
  26.  
  27.     int &r = *pi;
  28.     cout << "r (0x" << &r << ") = " << r << endl;
  29.  
  30.     r = 11;
  31.     cout << "y (0x" << &y << ") = " << y << endl;
  32.  
  33.     float v[ ] = { 3.4, 1.2, 4.9 };
  34.     cout << "v[ 1 ] = " << index( v, 1 ) << endl;
  35.     index( v, 1 ) = 7.2;
  36.     cout << "v[ 1 ] = " << index( v, 1 ) << endl;
  37.  
  38.     const float *pf = new float( 3.14159 );
  39.     cout << "pf (0x" << pf << ") = " << *pf  << endl;
  40.     delete pf;  // shouldn't be possible!
  41.  
  42.     float &(*pindex)(float *, int ) = &index;
  43.     pf = &pindex( v , 0 );
  44.     cout << "pf (0x" << pf << ") = " << *pf  << endl;
  45. }

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].