By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

Newbee question : confused about Typedef

P: n/a
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 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
>
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 '06 #2

P: n/a
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 '06 #3

P: n/a
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.
<vc*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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.
Oct 23 '06 #4

P: n/a
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
Oct 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.