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

Int main()

{

Void (*SomethingToSay)(void);

return 0;

}

After declaring a pointer to a function, keep in mind that this declaration only creates a pointer, not an actual function. In order to use it, you must define the actual function that would carry the assignment the function is supposed to perform. That function must have the same return type and the same (number of) argument(s), if any. For example, the above declared pointer to function is of type void and it does not take any argument. you can define a function as follows:

Void MovieQuote()

{

Console::WriteLine("We went through a lot of trouble because

of you!");

Console::WriteLine("You owe us\n");

Console::WriteLine(" From \"Disorganized Crime\"");

}

With such an associated function defined, you can assign it to the name of the pointer to function as follows

SomethingToSay = MovieQuote;

This assignment gives life to the function declared as pointer. The function can then be called as if it had actually been defined. Here is an example:

using namespace System;

Void MovieQuote()

{

Console::WriteLine("We went through a lot of trouble because

of you");

Console::WriteLine("You owe us");

Console::WriteLine(" From \"Disorganized Crime\"");

}

Int main()

{

Void (*SomethingToSay)();

// Assign the MovieQuote() function to the pointer to function

SomethingToSay = MovieQuote;

// Call the pointer to function as if it had been defined already

SomethingToSay();

return 0;

}

This would produce:

We went through a lot of trouble because of you

You owe us

From "Disorganized Crime"

15.1.3A Function Pointer that Returns a Value

You can also declare a pointer to function for a function that returns a value. Remember that both functions must return the same type of value. Here is an example:

using namespace System;

Int Addition()

{

int a = 16, b = 442;

return a + b;

}

Int main()

{

Int (*SomeNumber)();

// Assign the MovieQuote() function to the pointer to function

SomeNumber = Addition;

// Call the pointer to function as if it had been defined already

Console::WriteLine("The number is {0}", SomeNumber());

return 0;

}

15.1.4A Function Pointer With Arguments

If you want to use a function that takes arguments, when declaring the pointer to function, provide the return type and an optional name for each argument. Here is an example:

int (*SomeNumber)(int x, int y);

When defining the associated function, besides returning the same type of value, make sure that the function takes the same number of arguments. Here is an example:

using namespace System;

int Addition(int a, int b)

{

return a + b;

}

int main()

{

int (*SomeNumber)(int x, int y);

int x = 128, y = 5055;

// Assign the MovieQuote() function to the pointer to function

SomeNumber = Addition;

// Call the pointer to function as if it had been defined already

Console::WriteLine("{0} + {1} = {2}\n", x, y, SomeNumber(x, y));

return 0;

}

15.1.5Type-Defining a Function Pointer

You can create a programmer-defined type as a pointer to function. Here is the syntax to use:

typedef (*TypeName)(Arguments);

The typedef keyword must be used.

The asterisk and the TypeName must be enclosed in parentheses. The name must follow the rules applied to functions so far.

The TypeName must be followed by parentheses. If the pointer to function will take arguments, provide its type or their types between parentheses. Otherwise, you can leave the parentheses empty (but you must provide the parentheses).

After creating such a custom type, the name of the type would be used as an alias to a pointer to function. Consequently, it can be used to declare a pointer to function. Here is an example:

using namespace System;