12
Ref
- #include <iostream>
- using namespace std;
- float &index( float *v, int i )
- {
- return v[ i ];
- }
- int main() {
- int x = 33;
- int &y = x;
- cout << "x (0x" << &x << ") = " << x << endl;
- cout << "y (0x" << &y << ") = " << y << endl;
- y = 99;
- cout << "y (0x" << &y << ") = " << y << endl;
- cout << "x (0x" << &x << ") = " << x << endl;
- int *pi = &x;
- cout << "pi (0x" << &pi << ") = 0x" << pi << " = " << *pi << endl;
- int &r = *pi;
- cout << "r (0x" << &r << ") = " << r << endl;
- r = 11;
- cout << "y (0x" << &y << ") = " << y << endl;
- float v[ ] = { 3.4, 1.2, 4.9 };
- cout << "v[ 1 ] = " << index( v, 1 ) << endl;
- index( v, 1 ) = 7.2;
- cout << "v[ 1 ] = " << index( v, 1 ) << endl;
- const float *pf = new float( 3.14159 );
- cout << "pf (0x" << pf << ") = " << *pf << endl;
- delete pf; // shouldn't be possible!
- float &(*pindex)(float *, int ) = &index;
- pf = &pindex( v , 0 );
- cout << "pf (0x" << pf << ") = " << *pf << endl;
- }
Comments