473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Newbee question : confused about Typedef

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
4 1411
>
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dusty | last post by:
I have several years experience using Delphi writing apps in windoz (not professional) and now in a effort to completely remove windoz, I am trying to re-write my apps using Python, PyQt and Qt...
5
by: Adam | last post by:
Hi, Me very confused. I have some XML that I want to convert to a more basic XML. I have put an example of what I have and what I want, I have used XSL to convert XML to HTML, but never this way. ...
4
by: EMW | last post by:
I've posted serveral times to get an answer, but till now still now satisfying answers. Here is my problem: I have filled a datagrid control with the contents of an Excel sheet. Over 3500...
2
by: Newbee Adam | last post by:
some said that .NET app can run on any program where rutime exists. What is "runtime" in this sense? will I have to install runtime or .net framework or .NET support on an xp machine for a...
5
by: ma740988 | last post by:
Select parameters in a vendors API file is as follows: #ifdef __cplusplus typedef void (*VOIDFUNCPTR)(...); /* ptr to function returning void */ #else typedef void (*VOIDFUNCPTR)(); ...
1
by: Raymond Du | last post by:
Hi, I am confused about when to use bracket and parenthesis, say the following code snippet: for (int i=0; i< Request.Form.Count; i++) Label1.Text = Label1.Text + Request.Form.GetKey(i) + "...
20
by: mechanicfem | last post by:
I thought (as they're in c99) that flexible arrays were there for a number of reasons - but I also thought they'd be great for stepping into structures (say) that were aligned in memory after (say)...
7
by: Pep | last post by:
I'm getting weird results at the moment so thought I'd rebase my knowledge of c++ storage with your help :) I have a class used as a type in a struct and the struct is handled by a 3rd party...
11
by: Juha Nieminen | last post by:
I'm writing an STL-style data container, and this problem is puzzling me. The following code should demonstrate the problem: //---------------------------------------------------------- #include...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.