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

6.3.1Practical Learning: Allocating Memory on the Native Heap

  1. To allocate memory on the heap, change the main() function as follows:  

using namespace System;

public value class CProperty

{

public:

__wchar_t TypeOfHome;

Int Bedrooms;

float Bathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty *condominium = new CProperty;

condominium->YearBuilt = 2000;

condominium->Bathrooms = 1.0f;

condominium->Stories = 1;

condominium->Value = 224825;

condominium->Bedrooms = 1;

condominium->TypeOfHome = L'C';

Console::WriteLine("=//= Altair Realty =//=");

Console::WriteLine("-=- Properties Inventory -=-");

Console::Write("Type of Home: ");

Console::WriteLine(condominium->TypeOfHome);

Console::Write("Number of Bedrooms: ");

Console::WriteLine(condominium->Bedrooms);

Console::Write("Number of Bathrooms: ");

Console::WriteLine(condominium->Bathrooms);

Console::Write("Number of Stories: ");

Console::WriteLine(condominium->Stories);

Console::Write("Year Built: ");

Console::WriteLine(condominium->YearBuilt);

Console::Write("Monetary Value: ");

Console::WriteLine(condominium->Value);

Console::WriteLine();

return 0;

}

  1. Execute the application to see the result

  2. Close the DOS window

6.3.2De-Allocating Memory from the Native Heap

After using a variable that was declared using the new operator, you can reclaim the memory space it was using. This is done with the delete operator.

Here is an example:

using namespace System;

public value struct CSquare

{

double Side;

};

Int main()

{

CSquare *square = new CSquare;

square->Side = 24.48;

Console::WriteLine("Square Characteristics");

Console::Write("Side: ");

Console::WriteLine(square->Side);

delete square;

Console::WriteLine();

return 0;

}

6.3.3Practical Learning: De-Allocating Memory from the Native Heap

  1. To de-allocate memory, change the main() function as follows:

using namespace System;

public value class CProperty

{

public:

__wchar_t TypeOfHome;

Int Bedrooms;

float Bathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty *condominium = new CProperty;

. . .

delete condominium;

Console::WriteLine();

return 0;

}

  1. Execute the application to see the result

  2. Close the DOS window

6.4Classes and Pointers to Pointers

6.4.1Declaring a Pointer to Pointer

In Lesson 2, we saw how to declare a pointer to a pointer to a primitive type. In the same way, you can declare a pointer to a pointer to a class. Here is an example:

using namespace System;

public value struct CSquare

{

double Side;

};

Int main()

{

CSquare **square;

Console::WriteLine();

return 0;

}

One way you can use such a variable is to initialize it with a reference to a pointer of the same type. This could be done as follows:

using namespace System;

public value struct CSquare

{

double Side;

};

Int main()

{

CSquare *sqr = new CSquare;

CSquare **square;

square = &sqr;

Console::WriteLine();

return 0;

}

Remember that, for a double-pointer to a class, such as **square, the expression *square is a pointer to the class. This means that you can use the single asterisk to access the (public) members of the object. To do this, you can use the arrow -> operator. Because the -> operator has higher precedence than the asterisk, you must isolate *square by including it in parentheses. This would be done as follows:

using namespace System;

public value struct CSquare

{

double Side;

};