Thread: Hi, a strange typedef issue!

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    108

    Lightbulb Hi, a strange typedef issue!

    Hi there!

    Check out this code:

    Code:
    extern "C" {
       HRESULT CreateRenderDevice(HINSTANCE hDLL, ZFXRenderDevice **pInterface);
       typedef HRESULT (*CREATERENDERDEVICE)(HINSTANCE hDLL, ZFXRenderDevice **pInterface);
       
       HRESULT ReleaseRenderDevice(ZFXRenderDevice **pInterface);
       typedef HRESULT (*RELEASERENDERDEVICE)(ZFXRenderDevice **pInterface);
       }
    The bit I'm interested in is in bold. I mostly get what a typedef is but this is something else! What the hell does it mean? Anyone got any ideas?

    p.s. I'm sorry I should add that this variable *CREATERENDERDEVICE ultimately becomes a pointer to a function returned by the windows function GetProcAddress when finding a function in a loaded DLL. I'm sorta ok with that bit, but just not the typedef bit. Thanks.
    Last edited by shrink_tubing; 05-22-2022 at 06:44 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,385
    It's the typedef of a function pointer.
    Consider
    Code:
    typedef int (*FUNC)(int, int);
    This is a typedef for a function that takes two ints and returns an int. The type is called FUNC.
    E.g., this function would have that type:
    Code:
    int my_func(int a, int b) { return a + b; }
    You could use that typedef in a program like this:
    Code:
    #include <iostream>
     
    typedef int (*FUNC)(int, int);
     
    int my_func(int a, int b) { return a + b; }
     
    int my_other_func(int a, int b) { return 2*a + 3*b; }
     
    void use_func(FUNC func) {
        std::cout << func(1, 2) << '\n';
    }
     
    int main() {
        use_func(my_func);
        use_func(my_other_func);
    }
    So your typedef
    Code:
       typedef HRESULT (*CREATERENDERDEVICE)(HINSTANCE hDLL, ZFXRenderDevice **pInterface);
    is a function type that takes a HINSTANCE and a ZFXRenderDevice** and returns an HRESULT. The type is called CREATERENDERDEVICE.

    p.s., Just saw your p.s. I should perhaps add that part of the strange look of the thing is the parentheses around the asterisk and the typedef name. Those are needed to tell the compiler that the asterisk goes with the name and not the return type.

    In C++, such a typedef can be written as a using statement. This makes the name more prominent.
    Code:
    using FUNC = int(*)(int, int);
    Although you presumably can't put that in an extern "C" block.
    Last edited by john.c; 05-22-2022 at 06:56 PM.
    A long time ago being crazy meant something. - Charles Manson

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    108
    Bloody Hell John I couldn't have asked for better than and so fast as well. Thanks very much John that clears it up for me thanks

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,385
    Apparently you can also write the typedef like this, without the parentheses and asterisk:
    Code:
    typedef int FUNC(int, int);
    You can also leave the parentheses and asterisk out of the using alias:
    Code:
    using FUNC = int (int, int);
    I didn't know that you could do that. :-)

    Also, you can pass a function without using a typedef or using alias like so:
    Code:
    void use_func(int (*func)(int, int)) {
        // function pointer's name is func
        ,,,
    }
    or
    Code:
    void use_func(int func(int, int)) {
        // function pointer's name is func
        ,,,
    }
    But it's often cleaner to use a typedef or using alias.
    A long time ago being crazy meant something. - Charles Manson

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    108
    Hey thanks very much John much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef issue in c
    By shaswat in forum C Programming
    Replies: 5
    Last Post: 01-21-2020, 11:24 PM
  2. Issue with pointer to a const char using typedef
    By krkr in forum C Programming
    Replies: 3
    Last Post: 07-24-2015, 06:53 AM
  3. typedef issue
    By quo in forum C++ Programming
    Replies: 16
    Last Post: 05-09-2012, 03:05 PM
  4. typedef issue
    By Drac in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2009, 04:41 PM
  5. strange array issue
    By yaazz in forum C++ Programming
    Replies: 6
    Last Post: 05-09-2007, 11:23 PM

Tags for this Thread