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

Int main()

{

CSquare sqr;

sqr.Side = 24.48;

CSquare *square = &sqr;

Console::WriteLine("Square Characteristics");

Console::Write("Side: ");

Console::WriteLine((*square).Side);

Console::WriteLine();

return 0;

}

As an alternative, you can access the values using the pointer to the class. In this case, you would use the name of the pointer and the arrow operator. Here are examples:

using namespace System;

public value struct CSquare

{

double Side;

};

Int main()

{

CSquare sqr;

sqr.Side = 24.48;

CSquare *square = &sqr;

Console::WriteLine("Square Characteristics");

Console::Write("Side: ");

Console::WriteLine(square->Side);

Console::WriteLine();

return 0;

}

Both notations produce the same result:

Square Characteristics

Side: 24.48

6.2.3Practical Learning: Accessing the Members of a Pointer to a Class

  1. To access the members of a pointer to class, 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 apartment;

CProperty *condominium = &apartment;

condominium->YearBuilt = 1986;

condominium->Bathrooms = 1.5f;

condominium->Stories = 3;

condominium->Value = 348255;

condominium->Bedrooms = 3;

condominium->TypeOfHome = L'T';

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

6.3Allocating Memory on the Native Heap

Using a pointer allows you to use memory only as needed. This is done by using the asterisk * and the new operators. The syntax of declaring a pointer to a class is:

ClassName* VariableName = new ClassName;

The ClassName is the class whose variable you want to declare. It could be in the program or one that shipped with the compiler. Once again, the asterisk lets the compiler know that the object is declared as a pointer. The variable name follows the same rules we have applied to other variables so far.

After declaring the pointer, you can access each of its members and assign it an appropriate value. Here are examples:

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);

Console::WriteLine();

return 0;

}