Another basic C++ question

Started by
4 comments, last by LukeSkyRunner 20 years ago
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?
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
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}; 


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?
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
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.

This topic is closed to new replies.

Advertisement