typedef struct identifiers



 DEVELOP > c-Plus-Plus > typedef struct identifiers

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 05 May 2006 02:41:45 PM
Object: typedef struct identifiers
I notice that sometimes typedef struct definitions have an additional
"prefix" identifier like so:
typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;
What is the rationale for the extra name and what are the implications
of leaving it off?
.

User: "Jonathan Mcdougall"

Title: Re: typedef struct identifiers 05 May 2006 02:57:58 PM
wrote:

I notice that sometimes typedef struct definitions have an additional
"prefix" identifier like so:

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

What is the rationale for the extra name and what are the implications
of leaving it off?

This is a relic from C, where structs had to be explicitly qualified
when instantiated:
struct A {};
// A a; <-- illegal
struct A a; // ok
A workaround was to typedef the struct. This is not necessary in C++.
Don't do it.
Jonathan
.
User: "Marcus Kwok"

Title: Re: typedef struct identifiers 05 May 2006 03:39:38 PM
Jonathan Mcdougall <jonathanmcdougall@gmail.com> wrote:

godfatherofsoul@yahoo.com wrote:

I notice that sometimes typedef struct definitions have an additional
"prefix" identifier like so:

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

What is the rationale for the extra name and what are the implications
of leaving it off?


This is a relic from C, where structs had to be explicitly qualified
when instantiated:

struct A {};

// A a; <-- illegal
struct A a; // ok

A workaround was to typedef the struct. This is not necessary in C++.
Don't do it.

I agree with everything Jonathan said, however I will provide a little
more information.
When you do:
typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;
This creates a new name (Name_t) for the type "struct prefixName". If
you leave off "prefixName", then it creates a new name (Name_t) for the
unnamed struct with two char members.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
.
User: "Tomás"

Title: Re: typedef struct identifiers 05 May 2006 07:28:28 PM

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

This creates a new name (Name_t) for the type "struct prefixName". If
you leave off "prefixName", then it creates a new name (Name_t) for the
unnamed struct with two char members.

Slightly off-topic... but I have a question.
If you do the following in C:
struct Monkey { int i; };
Then you have to declare objects of it like so:
struct Monkey object;
But if you do the following:
typedef struct Monkey { int i; } Ape;
Then can you declare an object of it as follows:
struct Ape object; //using "struct" prefix
Or can you only do:
Ape object;
I presume that you'd still be able to write:
struct Monkey object;
but NOT:
Monkey object;
-Tomás
.
User: "Jonathan Mcdougall"

Title: Re: typedef struct identifiers 05 May 2006 07:59:40 PM
Tom=E1s wrote:

typedef struct prefixName
{
char firstName;
char lastName;
} Name_t;

This creates a new name (Name_t) for the type "struct prefixName". If
you leave off "prefixName", then it creates a new name (Name_t) for the
unnamed struct with two char members.


Slightly off-topic...

Not at all.

but I have a question.

If you do the following in C:

struct Monkey { int i; };

Then you have to declare objects of it like so:

struct Monkey object;

Yes.

But if you do the following:

typedef struct Monkey { int i; } Ape;

Then can you declare an object of it as follows:

struct Ape object; //using "struct" prefix

No, you cannot. That's the beauty of a typedef: you don't care what's
the underlying type. It is not necessary to put "struct" before it and
what's not necessary is mostly illegal in C and C++.

Or can you only do:
Ape object;

Yes.

I presume that you'd still be able to write:
struct Monkey object;

Yes.

but NOT:
Monkey object;

That's right.
Jonathan
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER