Chapter 32. Recursive Declarations

Difficulty: 6

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

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

  2. 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.

  3. 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 ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.