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

25.4Arrays and Functions

25.4.1 Returning an Array

Instead of returning a single value, a function can be made to return an array of values. When declaring the function, to indicate that it returns an array, precede its name with the array handle declaration. Here is an example:

array<int> ^ GetStories();

When implementing the function, you can do what is necessary but before exiting the body of the function, make sure you return an array of handles of the appropriate type. For example, in the body of the function, you can declare an array of handles, initialize it, and then return it. Here is an example:

array<int> ^ GetStories()

{

array<int> ^ Stories = gcnew array<int>(5);

Stories[0] = 4;

Stories[1] = 2;

Stories[2] = 3;

Stories[3] = 1;

Stories[4] = 3;

return Stories;

}

When calling the function, remember that, since it returns an array of handles, you can assign it to such a variable. Here is an example:

using namespace System;

array<int> ^ GetStories()

{

array<int > ^ Stories = gcnew array<int>(5);

Stories[0] = 4;

Stories[1] = 2;

Stories[2] = 3;

Stories[3] = 1;

Stories[4] = 3;

return Stories;

}

Int main()

{

array<int > ^ Stories = GetStories();

Console::WriteLine(L"Levels: {0}", Stories[0]);

Console::WriteLine(L"Levels: {0}", Stories[1]);

Console::WriteLine(L"Levels: {0}", Stories[2]);

Console::WriteLine(L"Levels: {0}", Stories[3]);

Console::WriteLine(L"Levels: {0}", Stories[4]);

Console::WriteLine();

return 0;

}

This would produce:

Levels: 4

Levels: 2

Levels: 3

Levels: 1

Levels: 3

You can also make the function return an array of handles. To do this, between the <> operator, include the ^ operator. In this case also, remember to return an appropriate array that corresponds to the type on the left side of the function name. Here is an example:

using namespace System;

// This function must return an array of handles

array<int ^> ^ GetStories()

{

array<int ^> ^ Stories = gcnew array<int ^>(5);

Stories[0] = gcnew int(4);

Stories[1] = gcnew int(2);

Stories[2] = gcnew int(3);

Stories[3] = gcnew int(1);

Stories[4] = gcnew int(3);

return Stories;

}

int main()

{

array<int ^> ^ Stories = GetStories();

Console::WriteLine(L"Levels: {0}", Stories[0]);

Console::WriteLine(L"Levels: {0}", Stories[1]);

Console::WriteLine(L"Levels: {0}", Stories[2]);

Console::WriteLine(L"Levels: {0}", Stories[3]);

Console::WriteLine(L"Levels: {0}", Stories[4]);

Console::WriteLine();

return 0;

}

Because you can initialize an array using its own curly brackets, the array in the above function could also be initialized as follows:

// This function must return an array of handles

array<int ^> ^ GetStories()

{

array<int ^> ^ Stories = gcnew array<int ^>(5)

{

gcnew int(4),

gcnew int(2),

gcnew int(3),

gcnew int(1),

gcnew int(3)

};

return Stories;

}

25.4.2Practical Learning: Returning Arrays

  1. To return arrays from functions, change the program as follows:  

    using namespace System;

    array<int ^> ^ GetBedrooms();

    array<Byte> ^ GetStories();

    int main()

    {

    array<__wchar_t> ^ HouseTypes = gcnew array<__wchar_t>(4)

    { L'S', L'C', L'S', L'T' };

    array<int ^> ^ Bedrooms = GetBedrooms();

    array<Byte> ^ Stories = GetStories();

    array<double> ^ Bathrooms = gcnew array<double>(4)

    { 3.5, 1, 2.5, 1.5 };

    array<bool> ^ HasGarage = gcnew array<bool>(4)

    { true, true, false, false };

    array<double ^> ^ Values = gcnew array<double ^>(4)

    {

    gcnew double(550500.00),

    gcnew double(115000.00),

    gcnew double(425000.00),

    gcnew double(350000.00)

    };

    Console::WriteLine(L"House Catalog");

    Console::WriteLine(L"===================================================");

    Console::WriteLine(L"Type Bedrooms Stories Bathrooms Garage Value");

    Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");

    for(int house = 0; house < 4; house++)

    {

    Console::WriteLine(L" {0}\t{1}\t{2}\t{3}\t{4}\t{5:F}",

    HouseTypes[house], Bedrooms[house],

    Stories[house], Bathrooms[house],

    HasGarage[house], Values[house]);

    Console::WriteLine(L"---------------------------------------------------");

    }

    Console::WriteLine();

    return 0;

    }

    array<int ^> ^ GetBedrooms()

    {

    array<int ^> ^ beds = gcnew array<int ^>(4);

    beds[0] = 5;

    beds[1] = 2;

    beds[2] = 3;

    beds[3] = 1;

    return beds;

    }

    array<Byte> ^ GetStories()

    {

    array<Byte > ^ Stories = gcnew array<Byte>(4);

    Stories[0] = 4;

    Stories[1] = 2;

    Stories[2] = 3;

    Stories[3] = 1;

    return Stories;

    }

  2. Execute the application to test it

  3. Close the DOS window and return to your programming environment