CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2010
    Posts
    16

    Post typedef a function question

    typedef int (*function)(int,double);

    could anyone explain how typedef'ing a function works? and why someone would use this?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: typedef a function question

    First note that you can only typedef a function pointer, not the function itself.

    I, personally, really prefer typedefing function pointers because I hate their declaration syntax and want to use it as rarely as possible.

    Function pointers were, for instance, used in classical C to implement some kind of polymorphism, which nowadays, in C++, is done using virtual functions. (And, BTW, these virtual functions are usually implemented internally in terms of function pointers by the compiler.) It is a way of specifying an object's behaviour at runtime.

    Look at this small example:

    Code:
    #include <iostream>
    
    struct math_operation;  // Forward declaration
    
    typedef int (*math_operator)(math_operation *);  // The function pointer typedef
    
    struct math_operation {
      math_operator op;  // Operator function
      int operand_l, operand_r;  // Two operands
    };
    
    int op_add(math_operation *operation)  // Add operator
    {
      return operation->operand_l + operation->operand_r;
    }
    
    int op_subtract(math_operation *operation)  // Subtract operator
    {
      return operation->operand_l - operation->operand_r;
    }
    
    int main()
    {
      math_operation add, sub;
    
      // Set up addition
    
      add.op = op_add;  // Take address of operator function
      add.operand_l = 2;
      add.operand_r = 3;
    
      // Set up subtraction
    
      sub.op = op_subtract;
      sub.operand_l = 4;
      sub.operand_r = 5;
    
      // Evaluate the two operation structs
    
      std::cout << "Result of the addition: " << add.op(&add) << std::endl;
      std::cout << "Result of the subtraction: " << sub.op(&sub) << std::endl;
    
      return 0;
    }
    The example is drastically simplified and as such not of much practical use. But I once wrote a math expression parser that built a tree of similar structures, and the entire expression could later be evaluated by simply kicking off evaluation of the root node. (But that involved a lot of void pointers and evil casting, and it was definitely anti-C++ style. )

    Note that the operator function in the example gets passed a pointer to the struct it belongs to in order to find its operands. This pointer is passed implicitly in C++ and is available under the keyword this inside a member function. (And it is dereferenced implicitly by simply using the names of class members.)

    Another very common use of function pointers, at least under Windows, is accessing functions inside a DLL if the DLL is linked in at runtime (as opposed to load-time linking). The GetProcAddress() API function used to obtain a function's address returns a function pointer, and its return type FARPROC in fact is a typedef for that.

    HTH
    Last edited by Eri523; December 1st, 2010 at 11:57 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: typedef a function question

    Quote Originally Posted by Eri523 View Post
    Another very common use of function pointers, at least under Windows, is accessing functions inside a DLL if the DLL is linked in at runtime (as opposed to load-time linking). The GetProcAddress() API function used to obtain a function's address returns a function pointer, and its return type FARPROC in fact is a typedef for that.
    Very good post by Eri523. I'd like to expand just a bit on this one statement.

    One common use of the typedef / LoadLibrary() / GetProcAddress() method is to support the latest operating system features in your software while still being compatible with older systems.

    For example, if you hard-code an API function that is only available in Windows Vista and 7, then your program will not load on XP and earlier systems. If you use the typedef method then you can support the newer features when your program is running on Vista/7 and provide a less modern alternative for older systems.

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: typedef a function question

    Quote Originally Posted by Eri523 View Post
    First note that you can only typedef a function pointer, not the function itself.
    I'm pretty sure you can:

    Code:
    typedef int Function(int,double);
    //...
    Function* m_function;
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: typedef a function question

    Quote Originally Posted by Zaccheus View Post
    I'm pretty sure you can:

    [...]
    Oops! Looks perfectly reasonable. I haven't seen that ever being done anywhere though in 20+ years of C and C++, and thus simply didn't consider that variant. Thanks for correction.
    Last edited by Eri523; November 16th, 2010 at 10:42 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: typedef a function question

    You can also have references to functions too.
    Code:
    typedef int (&Function)(int, double);
    This has the disadvantage over pointers in that the reference cannot be reseated like a pointer can, nor can it be NULL, but if you never need pass in a NULL or need to reseat the pointer then use a reference instead.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: typedef a function question

    If you want to be able to support functors in addition to functions, then you have two options. If you don't need to store the function/functor persistently anywhere, then you just use a template to deduce its type. If you do need to store it, then you can use a std::function<int (int, double)> (or a boost::function on older compilers) which is capable of storing any function pointer or functor with a given signature.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured