Jump to content
  • Advertisement

Archived

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

LukeSkyRunner

Another basic C++ question

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

What is the difference between: typedef struct _STR { char *str; int numchars; } STR; and the "basic" structure declaration? what _STR will be? What is the difference between _STR and STR?

Share this post


Link to post
Share on other sites
Advertisement
In C, this is a way to define a struct so that you can refer to it thereafter in code without the struct keyword. In C++, the two methods are more or less identical. There''s very little reason to use this idiom in C++.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Nothing really, not in c++ anyway. If I recall correctly, in the old days of C programming you had to typedef you structs if you wanted to declare more than one. For example



typdef struct //you don''t actually need _STR
{
char* str;
int numchars
}STR;

// now you can declare them as:

STR foo;
STR bar;

// but in c++ the following is the equivalent of the above:

struct STR
{
char* str;
int numchars
};




Share this post


Link to post
Share on other sites
Thanks!

You said they are almost identical, so, what is the small difference?
Whit this method, can I use _STR instead of STR identifier to declare a structure?

Share this post


Link to post
Share on other sites
The small difference is that the C way really screws things up if you want to do templated structs. You can indeed use either _STR or STR if you do it that way.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
There are also small differences in exported symbol names (only really important if you''re writing a linker), valid member function names (not important if you''re using the struct as POD) and namespace pollution.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!