Item 32. Recursive Declarations

I l @ ve RuBoard

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

I l @ ve RuBoard


More Exceptional C++
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
ISBN: 020170434X
EAN: 2147483647
Year: 2001
Pages: 118
Authors: Herb Sutter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net