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

Int main()

{

CSquare *sqr = new CSquare;

CSquare **square;

square = &sqr;

(*square)->Side = 24.48;

Console::WriteLine("Square Characteristics");

Console::Write("Side: ");

Console::WriteLine((*square)->Side);

delete sqr;

Console::WriteLine();

return 0;

}

You may remember that the name of a pointer variable preceded with the asterisk represents the value held by the variable. When applied to a class, for a single pointer, the name preceded with * is the value of the variable. For a double-pointer, the name preceded with two asterisks is the value held by the pointer to pointer. This allows you to access the members of the class using the period operator. The above program can be written as:

using namespace System;

public value struct CSquare

{

double Side;

};

Int main()

{

CSquare *sqr = new CSquare;

CSquare **square;

square = &sqr;

(**square).Side = 24.48;

Console::WriteLine("Square Characteristics");

Console::Write("Side: ");

Console::WriteLine((*square)->Side);

delete sqr;

Console::WriteLine();

return 0;

}

Just as demonstrated in Lesson 2, after initializing a pointer, if you change the value of the variable it points to, the pointer would be updated. If the variable was declared as a pointer to pointer and if you change either the pointer it points to, the pointer to pointer would be updated.

6.5Handle Types

6.5.1 Introduction

When studying pointers, we saw that the compiler reserved an area in heap memory for the pointers of your application. We also saw that, when you declared a pointer and allocated memory using the new operator, you should remember to de-allocate the memory using the delete operator. Although modern compilers are equipped to check this, if you are working with one that would not empty the memory for you, your program could create a memory leak. The error being human, this has been one of the regular concerns when programming with C and C++. One of the improvements of C++/CLI was to assist you with this.

If you (decide to) use pointers in your program, the heap memory is highly under your control. Notice that we use "if". This means that, in C++, you can create a complete program that doesn't, or hardly, use pointers. For a serious programmer, there is no way you can simply decide not to use pointers at all. Since you are going to use them, as we mentioned, the compiler reserves some heap memory for you. If you don't manage it right, you can create a mess. To avoid this, no more "if", the common language runtime (CLR) suggests and sometimes requires that you use another technique. This time, instead of your being in control of that heap, the compiler sets aside its own portion of heap and becomes in charge of it. To differentiate both portions of memory, the heap we mentioned when studying pointers is referred to as native heap. The heap used by the CLR is called the managed heap:

Computer Memory