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

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty * victorian = new CProperty;

victorian->Value = 485565;

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

Console::WriteLine(victorian->Value);

delete victorian ;

Console::WriteLine();

return 0;

}

If you want, you can store the variable in the managed heap by creating it as a handle:

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 = gcnew CProperty;

victorian->Value = 485565;

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

Console::WriteLine(victorian->Value);

Console::WriteLine();

return 0;

}

As seen already, you can also create a reference to a class type. This means that you have a choice of storing the variable in the stack, the native heap, or the managed heap. Besides the value keyword, you can create a class using the ref keyword. Here is an example:

public ref class CProperty

{

public:

__wchar_t TypeOfHome;

int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

int YearBuilt;

double Value;

};

In most cases, you can use a ref class as you would a value class. There are still many differences between both types. One of the rules you must observes is that you cannot create a native reference of ref class. For example, the following program will compile:

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;

CProperty & single = victorian;

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

Console::WriteLine(victorian.Value);

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

Console::WriteLine(single.Value);

Console::WriteLine();

return 0;

}

The following will not compile:

using namespace System;

public ref class CProperty

{

public:

__wchar_t TypeOfHome;

Int NumberOfBedrooms;

double NumberOfBathrooms;

Byte Stories;

Int YearBuilt;

double Value;

};

Int main()

{

CProperty victorian;

victorian.Value = 485565;

CProperty & single = victorian;

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

Console::WriteLine(victorian.Value);

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

Console::WriteLine(single.Value);

Console::WriteLine();

return 0;

}

When compiling it in Microsoft Visual C++ 2005, this program would produce a C3699, a C2440, and a C2228 errors:

error C3699: '&' : cannot use this indirection on type 'CProperty'

compiler replacing '&' with '^' to continue parsing

error C2440: 'initializing' : cannot convert from 'CProperty' to 'CProperty ^'

No user-defined-conversion operator available, or

No user-defined-conversion operator available that can perform

this conversion, or the operator cannot be called

.\Exercise.cpp(26) : error C2228: left of '.Value' must have class/struct/union

type is 'CProperty ^'

did you intend to use '->' instead?

The reason this will not compile is that, if you create a class as ref or if you use a class that was created as ref, you cannot create a native reference of it. If you want to create a reference and if you are the one creating a class, make it a value type. If you created the class as ref, or if you are using a ref class that was created by someone else, you can create a tracking reference of it. Based on this, the following instead will work:

using namespace System;

public ref class CProperty

{

public:

__wchar_t TypeOfHome;