• Advertisement
Sign in to follow this  

struct / typedef question

This topic is 4711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've always wondered about this but never bothered to ask... Does anyone know why some people do: struct MYSTRUCT { int someint; char somechar; etc... }; vs. typedef struct { int someint; char somechar; etc... } MYSTRUCT; The 2nd version always seemed a bit odd to me, since in C++ everything else has the names at top. (If you define a function, the name goes at the top, same with classes, etc.) So, is there any advantage to the 2nd version? Maybe it has something to do w/ compatibility with plain C compilers? Thanks, roos

Share this post


Link to post
Share on other sites
Advertisement
The typedef version is an old trick C programmers used, it's completely unnecessary in C++, (so if you're using C++ you should use the first version).

Apparently in C if you simply said
struct A{int something;};

Everytime you wanted to create an object of A you had to explicitly say "struct A" to let the compiler know "A" was a struct type. i.e. I believe you would have had to say
struct A a1;
Using the typedef method you remove the need to say "struct A" so you can simply do:
A a1;
like you do in C++.

Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

Share this post


Link to post
Share on other sites
Quote:
Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

I disagree with that. And apparently others do too... If you notice the structs declared in windows.h are all caps. Same with directx.

Share this post


Link to post
Share on other sites
Quote:
Original post by chad_420
Quote:
Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

I disagree with that. And apparently others do too... If you notice the structs declared in windows.h are all caps. Same with directx.


This may sound weird, but that was a major factor in my decision to learn SDL/OpenGL over Win32/DirectX[smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by roos
Hi,

I've always wondered about this but never bothered to ask... Does anyone know why some people do:

struct MYSTRUCT
{
int someint;
char somechar;
etc...
};

vs.

typedef struct
{
int someint;
char somechar;
etc...
} MYSTRUCT;

The 2nd version always seemed a bit odd to me, since in C++ everything else has the names at top. (If you define a function, the name goes at the top, same with classes, etc.)

So, is there any advantage to the 2nd version? Maybe it has something to do w/ compatibility with plain C compilers?

Thanks,
roos


The first is a C++ class (struct) definition. The second is the same, but without the class name. You're creating a struct (class) without a name, and declaring it a new type. Many compilers will list the second type as an unnamed struct.

"load_bitmap_file" explained the rest well.

Share this post


Link to post
Share on other sites
Quote:
Original post by Simian Man
Quote:
Original post by chad_420
Quote:
Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

I disagree with that. And apparently others do too... If you notice the structs declared in windows.h are all caps. Same with directx.


This may sound weird, but that was a major factor in my decision to learn SDL/OpenGL over Win32/DirectX[smile]


On a related note, ALLCAPS is the tool of the devil and lawyers. You know why they put the important stuff in allcaps? It's because they don't want you to read that stuff.

</conspiracytheorist>

In seriousness, most humans interpret words in large part based on the heights of their letters. With ALLCAPS, this is a constant, which slows comprehension and can induce strain. As such, I reserve ALLCAPS only for dirty things like macros.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement