More Exceptional C++

Все, что вы хотели знать о программизме, но боялись спросить.
Ответить
Аватара пользователя
Проф. Преображенский
Графоман
Сообщения: 20276
Зарегистрирован: 08 ноя 2006, 11:10

More Exceptional C++

Сообщение Проф. Преображенский »

http://anatolix.naumen.ru/files/books/m ... al_cpp.zip


Item 32. Recursive Declarations
Difficulty: 6

Can you write a function that returns a pointer to itself? If so, why would you want to?

What is a pointer to function? How can it be used?

Assume it is possible to write a function that can return a pointer to itself. That function could equally well return a pointer to any function with the same signature as itself. When might this be useful? Explain.

Is it possible to write a function f() that returns a pointer to itself? It should be usable in the following natural way:

// Example 32-3
//
// FuncPtr is a typedef for a pointer to a
// function with the same signature as f()
//
FuncPtr p = f(); // executes f()
(*p)(); // executes f()

If it is possible, demonstrate how. If it is not possible, explain why.
Ответить