Newbee question : confused about Typedef

Discussion in 'C++' started by vcinquini@gmail.com, Oct 23, 2006.

  1. Guest

    I've always learned about typedef as a statement that creates an alias
    for a type. For example:

    typedef long DWORD;

    but I'm confused about the following typedef. Which is the type and
    which is the alias?


    class MyClass
    {
    public:
    ....
    (some var declarations - doesn't matter here)...
    ....
    typedef UINT(__stdcall *tpf_ServThread)(void*);
    ....
    void ServerWaitConnections(tpf_ServThread pfThread);
    .....
    }

    in the .cpp

    void Sockets::ServerWaitConnections(tpf_ServThread pfThread)
    {
    struct sockaddr_in sadrRemote;
    int nEndLen = sizeof(sadrRemote);

    while ( m_Socket != INVALID_SOCKET )
    {
    SOCKET SockRemote = accept( m_Socket,
    (struct sockaddr*) &sadrRemote,
    &nEndLen );

    if ( SockRemoto == INVALID_SOCKET )
    break;

    UINT nThID;
    _beginthreadex( NULL,
    0,
    pfThread,
    (void*)SockRemote,
    0,
    &nThID );
    }
    CloseSocket(m_Socket);
    }
     
    , Oct 23, 2006
    #1
    1. Advertising

  2. Ron Natalie Guest


    >
    > but I'm confused about the following typedef. Which is the type and
    > which is the alias?
    >


    > typedef UINT(__stdcall *tpf_ServThread)(void*);


    Well it's partially confusing because of the bogus psuedo
    stroageclass __stdcall (that's a microsoftism) and the fact
    that UINT is already a typedef for unsigned int.

    So lets rewrite it to get rid of the off-topic microsoft drivel:

    typedef unsigned int (*tpf_ServThread)(void*);

    Now a typedef has the same syntax that a declaration would have
    except instead of declaring a variable, the variable name is
    the new type alias.

    unsigned int(*tpf_ServThread)(void*);

    Well the identifier is tpf_ServThread, that is what is being
    delared. Starting there we find that it has a * in front of
    it, that makes it a pointer. Then we look outside the parens
    we see (void*) on the right, then the pointer must refer to a function
    taking a void* parameter. Looking to the left we see unsigned
    int, that is the function return type.

    So tpf_ServThread is a pointer-to-function taking a parameter of
    type pointer-to-void and returning unsigned int.
     
    Ron Natalie, Oct 23, 2006
    #2
    1. Advertising

  3. Guest

    Can I assume that:

    void ServerWaitConnections(tpf_ServThread pfThread);

    can be read as:

    void ServerWaitConnections( unsigned int(*pfThread)(void*) );


    That Is it?


    Ron Natalie wrote:
    > >
    > > but I'm confused about the following typedef. Which is the type and
    > > which is the alias?
    > >

    >
    > > typedef UINT(__stdcall *tpf_ServThread)(void*);

    >
    > Well it's partially confusing because of the bogus psuedo
    > stroageclass __stdcall (that's a microsoftism) and the fact
    > that UINT is already a typedef for unsigned int.
    >
    > So lets rewrite it to get rid of the off-topic microsoft drivel:
    >
    > typedef unsigned int (*tpf_ServThread)(void*);
    >
    > Now a typedef has the same syntax that a declaration would have
    > except instead of declaring a variable, the variable name is
    > the new type alias.
    >
    > unsigned int(*tpf_ServThread)(void*);
    >
    > Well the identifier is tpf_ServThread, that is what is being
    > delared. Starting there we find that it has a * in front of
    > it, that makes it a pointer. Then we look outside the parens
    > we see (void*) on the right, then the pointer must refer to a function
    > taking a void* parameter. Looking to the left we see unsigned
    > int, that is the function return type.
    >
    > So tpf_ServThread is a pointer-to-function taking a parameter of
    > type pointer-to-void and returning unsigned int.
     
    , Oct 23, 2006
    #3
  4. Jim Langston Guest

    > Ron Natalie wrote:
    >> >
    >> > but I'm confused about the following typedef. Which is the type and
    >> > which is the alias?
    >> >

    >>
    >> > typedef UINT(__stdcall *tpf_ServThread)(void*);

    >>
    >> Well it's partially confusing because of the bogus psuedo
    >> stroageclass __stdcall (that's a microsoftism) and the fact
    >> that UINT is already a typedef for unsigned int.
    >>
    >> So lets rewrite it to get rid of the off-topic microsoft drivel:
    >>
    >> typedef unsigned int (*tpf_ServThread)(void*);
    >>
    >> Now a typedef has the same syntax that a declaration would have
    >> except instead of declaring a variable, the variable name is
    >> the new type alias.
    >>
    >> unsigned int(*tpf_ServThread)(void*);
    >>
    >> Well the identifier is tpf_ServThread, that is what is being
    >> delared. Starting there we find that it has a * in front of
    >> it, that makes it a pointer. Then we look outside the parens
    >> we see (void*) on the right, then the pointer must refer to a function
    >> taking a void* parameter. Looking to the left we see unsigned
    >> int, that is the function return type.
    >>
    >> So tpf_ServThread is a pointer-to-function taking a parameter of
    >> type pointer-to-void and returning unsigned int.


    <> wrote in message
    news:...
    > Can I assume that:
    >
    > void ServerWaitConnections(tpf_ServThread pfThread);
    >
    > can be read as:
    >
    > void ServerWaitConnections( unsigned int(*pfThread)(void*) );
    >
    >
    > That Is it?


    Please don't top post in this ng. Message rearranged.

    Yes. Although sometimes I find that I just can't get the compiler to accept
    a pointer to a function as a parameter without a typedef.
     
    Jim Langston, Oct 23, 2006
    #4
  5. Jim Langston posted:

    > Although sometimes I find that I just can't get the compiler to
    > accept a pointer to a function as a parameter without a typedef.



    Why is that? I've never had a problem compiling even the most elaborate of
    declarations.

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 24, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vbGansta
    Replies:
    0
    Views:
    303
    vbGansta
    Feb 16, 2004
  2. Valmir
    Replies:
    3
    Views:
    4,959
    xiko tripa
    Feb 18, 2004
  3. Who

    Newbee question

    Who, Apr 5, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    480
    Kevin Spencer
    Apr 5, 2004
  4. mbs

    Followup: Newbee Question

    mbs, Apr 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    309
    James Hancock
    Apr 6, 2004
  5. oor
    Replies:
    0
    Views:
    1,412
Loading...

Share This Page