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

4.4.2A Tracking Reference

In our introduction to pointers, we saw that a reference was a technique of declaring an additional variable that references a first variable. This was viewed as creating a variable that simply referred to another, primarily declared variable. Such a variable declared as a reference is stored in the stack. The compiler is then in charge of “cleaning” it, that is, in charge of reclaiming its memory when the reference is not used anymore. C++/CLI allows you to declare a reference that, if you want, would be stored in the CLR heap so the garbage collector would be in charge of managing it. Such a variable is called a tracking reference.

As done for a regular reference, you can also create a tracking reference of a primitive type. To do this, declare and initialize a variable. Here is an example:

double HourlySalary = 24.68;

To create a tracking reference, declare the variable but type the % operator between the data type and the name of the variable. Here is an example:

double % HourlyWage

To indicate the variable it is referencing, assign it the variable that was previously defined. Here is an example:

double % HourlyWage = HourlySalary;

After creating and initializing a tracking reference, you can access its value. You can access the variable and its reference using their names. Here is an example:

using namespace System;

Int main()

{

double HourlySalary = 24.68;

double % HourlyWage = HourlySalary;

Console::WriteLine("Payroll Preparation");

Console::Write("Hourly Salary: ");

Console::WriteLine(HourlySalary);

Console::Write("Hourly Wage: ");

Console::WriteLine(HourlyWage);

Console::WriteLine();

return 0;

}

The tracking reference and the variable it's referencing would always hold the same value: if you change the value of one, the other's would be changed also. This is illustrated in the following program:

using namespace System;

Int main()

{

double HourlySalary = 24.68;

double % HourlyWage = HourlySalary;

Console::WriteLine("Payroll Preparation");

Console::Write("Hourly Salary: ");

Console::WriteLine(HourlySalary);

Console::Write("Hourly Wage: ");

Console::WriteLine(HourlyWage);

HourlySalary = 15.25;

Console::WriteLine("\nNew Payroll Information");

Console::Write("Hourly Salary: ");

Console::WriteLine(HourlySalary);

Console::Write("Hourly Wage: ");

Console::WriteLine(HourlyWage);

Console::WriteLine();

return 0;

}

This would produce:

Payroll Preparation

Hourly Salary: 24.68

Hourly Wage: 24.68

New Payroll Information

Hourly Salary: 15.25

Hourly Wage: 15.25

4.4.3Creating a Handle (дескриптор)

Besides the native heap, when your application starts, the common language runtime (CLR) reserves another portion of memory that it is in charge of managing. This portion of memory is called the managed heap. To store a variable in the managed heap, you must create it as a handle. To create a handle, as opposed to the asterisk "*" of a pointer, you use the cap "^" operator. Here is an example:

using namespace System;