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

Int main()

{

double ^ ItemPrice;

Console::WriteLine();

return 0;

}

As mentioned for the ampersand & operator of the reference and the asterisk operator of the pointer, there are three basic positions the cap ^ can have:

double^ ItemPrice;

double ^ ItemPrice;

double ^ItemPrice;

The compiler will assume the same. In this case, ItemPrice is a handle to double. After creating a handle, the compiler reserves an amount of space in the managed heap memory for the variable but leaves that space empty. To access the value held by a handle, you can simply use its name. Here is an example:

using namespace System;

int main()

{

double ^ ItemPrice;

Console::WriteLine("Department Store");

Console::Write("Item Price: ");

Console::WriteLine(ItemPrice);

Console::WriteLine();

return 0;

}

This would produce:

Department Store

Item Price:

Notice that the handle holds a null value. When studying pointers, we mentioned that you could not initialize a newly created pointer with just any value. You could initialize it with 0, which represented the null value. To initialize a handle with a null value, you can assign it the nullptr value. Here is an example:

using namespace System;

int main()

{

double ^ ItemPrice = nullptr;

Console::WriteLine("Department Store");

Console::Write("Item Price: ");

Console::WriteLine(ItemPrice);

Console::WriteLine();

return 0;

}

This would produce:

Department Store

Item Price:

You assign the nullptr if you don't have or don't know the value that the handle holds. In this case, the compiler would only reserve an amount of space in the managed heap for the variable but it would leave it empty. After creating a handle, instead of keeping its memory empty, you can store a value in it. To do this, simply assign it a value, either when declaring the variable or after. Here is an example:

using namespace System;

Int main()

{

double ^ ItemPrice = nullptr;

Console::WriteLine("Department Store");

Console::Write("Item Price: ");

Console::WriteLine(ItemPrice);

ItemPrice = 148.95;

Console::Write("Marked Price: ");

Console::WriteLine(ItemPrice);

Console::WriteLine();

return 0;

}

This would produce:

Department Store

Item Price:

Marked Price: 148.95

4.5Using the Garbage Collector

We saw that there were some details related to using the native heap: you could hesitate to use or not use a pointer, you could forget to delete a pointer, you could even misuse it. To solve these and some other problems, the common language runtime reserves its own portion of the garbage collected heap to make it available when you use handles. Based on this, when creating a handle, to allocate memory for it, you can instantiate it using the gcnew operator. The formula to use it is:

DataType ^ VariableName = gcnew DataType;

The DataType is the type whose handle you want to create.

The ^, =, and gcnew operators are required.

The VariableName is the name of the variable that will be created.

Here is an example:

double ^ DiscountRate = gcnew double;

After creating the handle, you can access it using its name. Here is an example:

using namespace System;