typedefs

Started by
1 comment, last by Simian Man 15 years, 10 months ago
How come there oftern used in places like this? d3d9types.h, line 51

typedef struct _D3DVECTOR {
    float x;
    float y;
    float z;
} D3DVECTOR;

Rather than just having:

struct D3DVECTOR {
    float x;
    float y;
    float z;
};

I see it alot on enums as well...
Advertisement
Because those headers need to remain C compatible; "typedef struct" is a C idiom in that context, it prevents you from having to write "struct" in the declaration of a variable of that type (in C).
In C, you have to prepend structure names with the struct keyword. For example:
struct D3DVECTOR{    float x;    float y;    float z;};sturct D3DVECTOR vec;


To avoid this, C programmers often create typedefs so the struct names can be used directly. Of course using C++, we don't have that problem, but many C++ libraries are written with C interoperability in mind.

This topic is closed to new replies.

Advertisement