C++ Examples: |
|
Instead of returning a pointer or a reference, you can return a reference to a pointer. To declare such a function, precede its name with * and &. Here is an example: double *& ShowNumber() { } In the body of the function, you can do whatever is appropriate. An important rule with this type of function is that it must return either a global or a static variable. In other words, the variable that is returned must conserve its value when the function exits. Here is an example that returns a static variable: double *& ShowNumber() { double n = 1550.85; static double *v = &n; return v; } When calling the function, precede its name with an asterisk. Here is an example: //--------------------------------------------------------------------------- #include <iostream> using namespace std; double *& ShowNumber() { double n = 1550.85; static double *v = &n; return v; } //--------------------------------------------------------------------------- int main() { double Number = *ShowNumber(); cout << "Number: " << Number << endl; return 0; } //--------------------------------------------------------------------------- This would produce: Number: 1550.85 |
|
||
Home | Copyright © 2006-2016, FunctionX, Inc. | |
|