Archived

This topic is now archived and is closed to further replies.

HulkHogan

structs

Recommended Posts

I know that typedef helps me specify a type, Ex. typedef int integerhaha; integerhaha anotherinteger; But, how in the world does it relate to structs. Below.. typedef struct hero { int spidermanID; int batmanID; }; gave me the same result as... struct hero { int spidermanID; int batmanID; };

Share this post


Link to post
Share on other sites
It''s a C-only thing. You don''t need to do it for C++.

The reason is that if you do this in C...

struct SomeStruct
{
/* stuff */
};


...then you''d have to refer to it in code like this:

/* declare structures */
struct SomeStruct first;
struct SomeStruct second;


People are lazy, though, so this is a bother. The typedef works in the same way as with other things:

typedef /* lots of stuff...*/ my_typedef_name;

In the above code, the typedef is simply creating a new type so that you don''t need the "struct SomeStruct" part, instead allowing you to say "SomeStruct" only. It''s less typing.

However, it''s not necessary in C++. In C++, a struct is like a class except that it''s public by default. Therefore, the typedef is irrelevant - you don''t need to specify the "struct whatever" bit, since you can simply say "whatever".

Share this post


Link to post
Share on other sites
That''s correct ... the only difference (unless I missed something here) is that you no longer have to use the struct keywoard when declearing or defining or casting (ie from void *) to your hearo type

Share this post


Link to post
Share on other sites