Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

Int main()

{

int Number = 228;

int &Nbr = Number;

Console::WriteLine(Number);

Console::WriteLine(Nbr);

return 0;

}

The ampersand operator between the data type and the reference can assume one of three positions as follows:

int& Nbr;

int & Nbr;

int &Nbr;

As long as the & symbol is between a valid data type and a variable name, the compiler knows that the variable name (in this case Nbr) is a reference.

Once a reference has been initialized, it holds the same value as the variable it is pointing to. You can then display the value of the variable using either of both:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

int Number = 228;

int &Nbr = Number;

cout << "Number: ";

Console::WriteLine(Number);

cout << "Its reference: ";

Console::WriteLine(Nbr);

Console::WriteLine();

return 0;

}

This would produce:

Number: 228

Its reference: 228

Press any key to continue

If you change the value of the variable, the compiler updates the value of the reference so that both variables would hold the same value. In the same way, you can modify the value of the reference, which would update the value of the referred to variable. To access the reference, do not use the ampersand operator; just the name of the reference is sufficient to the compiler. This is illustrated in the following:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

int Number = 228;

int &Nbr = Number;

cout << "Number: ";

Console::WriteLine(Number);

cout << "Its reference: ";

Console::WriteLine(Nbr);

Number = -15008;

cout << "\nNumber: ";

Console::WriteLine(Number);

cout << "Its reference: ";

Console::WriteLine(Nbr);

Number = 28114;

cout << "\nNumber: ";

Console::WriteLine(Number);

cout << "Its reference: ";

Console::WriteLine(Nbr);

Console::WriteLine();

return 0;

}

This would produce:

Number: 228

Its reference: 228

Number: -15008

Its reference: -15008

Number: 28114

Its reference: 28114

Press any key to continue

It is very important to remember the relationship that a variable in the stack has with its value: a variable in the stack contains its own value. Whenever you access this variable, it gives you copy of its value and retains the original. Once you have that value, you can do what you want with it and this would not have any impact on the variable itself or its value. For example, if you assign such a value to another variable, that other variable receives its own copy. Let's illustrate with the following program:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

// TODO: Please replace the sample code below with your own.

int Number1 = 255;

int Number2 = Number1;

Console::WriteLine(Number1);

Console::WriteLine(Number2);

Console::WriteLine();

return 0;

}

This would produce:

255

255

Press any key to continue

Notice that when the value of the first variable has been assigned to the second variable both variables hold the same value. With another assignment, you can change the value of the second variable and the first would keep its value:

#include <iostream>

using namespace std;

using namespace System;