Jump to content
  • Advertisement

Archived

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

HulkHogan

structs

This topic is 5767 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

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!