Newbee question : confused about Typedef

V

vcinquini

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);
}
 
R

Ron Natalie

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

vcinquini

Can I assume that:

void ServerWaitConnections(tpf_ServThread pfThread);

can be read as:

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


That Is it?
 
J

Jim Langston

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

Frederick Gotham

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top