reftst2.cpp

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

CPP
49
字号
/*
    reftst2.cpp. Testing the reference operator
 */
#include    <stdio.h>
#include    <string.h>
// Define the structure
struct TESTER
{
    int     x;
    char    ch;
    char    str[27];
};
// Declare our functions
void FuncOne(int x, TESTER *t, int y);
void FuncOne(int x, TESTER &t, int y);

int main()
{
int a = 2, b = 3;
TESTER test = {1, 'X', "This is a string"};

    FuncOne (a, &test, b);     // Using pointer
    printf ("\ttest.x = %d\n", test.x);
    printf ("\ttest.ch = %c\n", test.ch);
    printf ("\ttest.str = %s\n", test.str);

    FuncOne (a, test, b);     // Using reference
    printf ("\ttest.x = %d\n", test.x);
    printf ("\ttest.ch = %c\n", test.ch);
    printf ("\ttest.str = %s\n", test.str);
    return (0);
}

void FuncOne(int x, TESTER *t, int y)
{
    printf ("Using normal variable call:\n");
    t->x = sizeof (TESTER);
    t->ch = 'B';
    strcpy (t->str, "Modified String");
}

void FuncOne(int x, TESTER &t, int y)
{
    printf ("Using reference variable call:\n");
    t.x = 2 * sizeof (TESTER);
    t.ch = 'C';
    strcpy (t.str, "Changed again, eh?");
}

⌨️ 快捷键说明

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