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

4.3.3Practical Learning: Introducing Pointers

  1. To start a new program, launch Microsoft Visual C++ 2005

  2. On the main menu, click File -> New -> Project...

  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project

  4. In the Name box, replace the string with RealEstate3 and click OK

  5. To create a source file, on the main menu, click Project -> Add New Item...

  6. In the Templates list, click Source File (.cpp)

  7. In the New box, type Exercise and click Add

  8. In the empty file, type:  

using namespace System;

Int main()

{

long *propertyNumber = new long;

__wchar_t *propertyType = new __wchar_t;

Byte *stories = new Byte;

unsigned int *bedrooms = new unsigned int;

float *bathrooms = new float;

unsigned int *yearBuilt = new unsigned int;

double *marketValue = new double;

*propertyNumber = 204755;

*propertyType = 'C';

*stories = 1;

*bedrooms = 2;

*bathrooms = 2.0F;

*yearBuilt = 2002;

*marketValue = 248550;

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

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

Console::Write("Property #: ");

Console::WriteLine(*propertyNumber);

Console::Write("Property Type: ");

Console::WriteLine(*propertyType);

Console::Write("Stories: ");

Console::WriteLine(*stories);

Console::Write("Bedrooms: ");

Console::WriteLine(*bedrooms);

Console::Write("Bathrooms: ");

Console::WriteLine(*bathrooms);

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

Console::WriteLine(*yearBuilt);

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

Console::WriteLine(*marketValue);

return 0;

}

  1. To execute the application, on the main menu, click Debug -> Start Without Debugging  

=//= Altair Realty =//=

-=- Properties Inventory -=-

Property #: 490724

Property Type: S

Stories: 2

Bedrooms: 5

Bathrooms: 3.5

Year Built: 1962

Market Value: 652540

  1. Close the DOS window

4.3.4The delete Operator

After dynamically allocating memory, you can assign a new value to the pointer for any purpose. Once the memory is not in use anymore, you should reclaim it. This is done with the delete operator. The formula to use it is:

delete PointerName;

The delete keyword is required. It lets the compiler know that you do not want to use the pointer anymore.

The PointerName is the name of the pointer that was dynamically allocated with the new operator. You cannot use the delete operator if you did not previously use the new operator to allocate memory.

To use the delete operator, simply type delete followed by the name of the pointer whose memory you want to reclaim. This operation is referred to as deallocating the memory (you are deallocating the memory that was dynamically allocated):

using namespace System;

Int main()

{

double * PHourly = new double;

Console::WriteLine("Stuff...");

delete PHourly;

return 0;

}

4.3.5Practical Learning: Deleting Pointers

  1. To de-allocate the memory previously used by the variables, change the file as follows:

using namespace System;