-3
using namespace boost;

typedef void (*PtrFunc)(any& );

how to understand above code sample about typedef in c++?

jiafu
  • 6,338
  • 12
  • 49
  • 73
  • It's defining a function pointer. – Nick May 09 '13 at 08:41
  • It's defining a type named `PtrFunc`: a pointer to a function which accepts a reference to `any` as its argument and returns `void`. – masoud May 09 '13 at 08:42
  • 1
    By reading a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which would explain how pointers to functions are formed. This one in particular makes `PtrFunc` a pointer to function taking `any&` and returning `void`). – Angew is no longer proud of SO May 09 '13 at 08:42

2 Answers2

1

This code is declaring a typedef called PtrFunc which is a function type which takes a single parameter of type boost::any&

You can use it like:

void myFunc(any&)
{
    ....
}

PtrFunc pointerToMyFunc = myFunc;
Mike Vine
  • 9,468
  • 25
  • 44
1

This is a pointer to a function returning void and accepting boost:any& as its sole argument.

It can be used like this:

void someFunction(any& arg)
{
    // ...
}

int main() {
    PtrFunc fn = someFunction;
    // ...
    fn(...);

    // You can also do this without a typedef
    void (*other_fn)(any&) = someFunction;
    other_fn(...);

    return 0;
}

See this article for a complete guide to reading type declarations in C (and, consequently, C++).

Also, this article provides some ASCII art!

Alexei Sholik
  • 7,287
  • 2
  • 31
  • 41