Skip to main content
GameDev.net gamedev.net
🔒 Locked

Another basic C++ question

Started by LukeSkyRunner Mar 24, 2004 at 3:02 PM 4 replies 300+ views
Original Post
LukeSkyRunner
LukeSkyRunner
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?
Sneftel
Sneftel
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
simba
simba
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
};




LukeSkyRunner
LukeSkyRunner
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?
Sneftel
Sneftel
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
SiCrane
SiCrane
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.