Thursday, January 6, 2011

typedef structs

Within the last couple years I've become a fan of Yossi Kreinin's C++ FQA, which goes a long way toward explaining the feeling I get often when coding C++: This would be easier in any other language.

Often that includes C. The superiority of some old C features and methodologies over the C++ ones meant to replace them, and the fact that they're usually still available in C++, has led to their frequent use in C++ code. The use of macros, pointer arithmetic, format strings, and the like, is to me often justified, even if they make a program look a lot different than what you see in a C++ reference book.

One C-ism I keep seeing and don't understand, however, is typedef struct. In C you had to typedef your struct types to refer to them without the struct keyword. C++ lifted this requirement, but I still see it all over the place. Of course, C++ also changed what a struct is considerably: it's the same as a class, but its members are public by default, not private. Sometimes I see structs written basically as mini-classes, with member functions and perhaps even constructors. In this case I don't see the C-ish typedef very often. Similarly, these ones are more likely to have “C++-like” member names.

C++ is a language that gives its users lots of flexibility in pointless places. Of course, it's typical for the state of software in 2010 that we aren't making intentional choices with that flexibility...

No comments: