Coding styles can be an emotive subject (along the lines of which is better, vi or emacs). I for one find the GNU recommended style to be horrible, preferring the guidance found in Documentation/CodingStyle (from any kernel source) to be reasonable.
On Thursday 11 August 2005 13:25, Stuart Bailey wrote:
Yes, but as far as I can remember (and I may be verging on C++), you can have functions within a struct. Therefore, you could create an initialisation function, with well named parameters (with defaults), that will setup the struct for you. Although you will still have a long parameter list. Unfortunately, I can't remeber the syntax - and I can't find any of my C books to reference. But basically, a C++ class is essentially a structure whose members are by default private rather than public as in structures.
struct Foo{ public: Foo() {a=0; b=2; c=3; bar="idunno"};
int a; int b; int c; char bar[80]; }
However, this only works with C++, so one small trick I use is a macro defined in the same header as the struct definition..
typedef struct { double s, x, y, z; } PmRotation;
#define PmRotation_init {1, 0, 0, 0}
In the source, it is a simple matter of doing:
PmRotation foo = PmRotation_init;
Once again, this gets messy if the multitude of structs needed different initialisation values...
Which is better - That really depends on how far you want to go in embracing C++ and OOP. Certainly C++ allows you to hide and protect many variables that would otherwise become global.
Regards, Paul.