reffunc.cpp

来自「这些源代码」· C++ 代码 · 共 41 行

CPP
41
字号
/*
    reffunc.cpp. An example of using reference
                 functions as lvalues.
 */
#include    <stdio.h>
#include    <iostream.h>

int IntArray[] = {1, 2, 3};
#define    SIZE    (sizeof(IntArray)/sizeof(int))

int& RefFunc(int x);

int main ()
{
int x;

    cout << "Before Reference Function: " << endl;
    for (x = 0; x < SIZE; ++x)
    {
        cout << "IntArray[" << x << "] = ";
        cout << IntArray[x] << endl;
    }
    for (x = 0; x < SIZE; ++x)
        RefFunc(x) = x * 42;

    cout << endl << "After Reference Function: " << endl;
    for (x = 0; x < SIZE; ++x)
    {
        cout << "IntArray[" << x << "] = ";
        cout << IntArray[x] << endl;
    }
    return (0);
}
//
// Return a reference to the array member indexed by
// parameter.
int& RefFunc (int x)
{
    return (IntArray[x]);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?