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

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty ^ townhome;

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 operator ^ can have:

CProperty^ townhome;

CProperty ^ townhome;

CProperty ^townhome;

The compiler will assume the same. In this case, townhome is a handle to CProperty.

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:

Int main()

{

CProperty ^ townhome = nullptr;

Console::WriteLine();

return 0;

}

After creating a handle, you should let the compiler know what variable it handles. To do this, assign it a declared variable preceded with the % operator, as we did for the & operator when referencing a pointer. Here is an example:

using namespace System;

public value class CProperty

{

public:

__wchar_t TypeOfHome;

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty townhouse;

townhouse.YearBuilt = 1986;

townhouse.NumberOfBathrooms = 1.5;

townhouse.Stories = 3;

townhouse.Value = 348255;

townhouse.NumberOfBedrooms = 3;

townhouse.TypeOfHome = L'T';

CProperty ^ townhome = %townhouse;

Console::WriteLine();

return 0;

}

6.5.5Practical Learning: Creating a Handle

  1. Access the Exercise.cpp file

  2. To create a handle, declare a CProperty variable as follows:

#include "Property.h"

using namespace System;

using namespace RealEstate;

Int main()

{

CProperty ^ house;

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

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

return 0;

}

  1. Save the Exercise.cpp file

6.6Reference Types

In Lesson 6, we saw that you could create a class using the value keyword on the left of the struct or class keyword. If you create a class using the value keyword, when you declare a variable from that class, we have seen so far that you could store it in the stack by declaring it as a value type. Here is an example:

using namespace System;

public value class CProperty

{

public:

__wchar_t TypeOfHome;

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty victorian;

victorian.Value = 485565;

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

Console::WriteLine(victorian.Value);

Console::WriteLine();

return 0;

}

Alternatively, you can store the variable in the native heap. To do this, declare it as a pointer. Remember that if you declare the variable as a pointer, it is stored in the native heap and you must remember to reclaim its memory area when the program ends. This is done using the delete operator:

using namespace System;

public value class CProperty

{

public:

__wchar_t TypeOfHome;