Structures - simple problem
Hi, I'm going hrough the NeHe OpenGL tutorials (which are excellent), and I'm up to tutorial 10.
I've just come across something which I've seen before, but never quite understood.
It concerns structures in c/c++.....
In the tutorial, structures are defined for a SECTOR, TRIANGLE and VERTEX, as such:
typedef struct tagSECTOR {
int numtriangles; TRIANGLE* triangle; } SECTOR;
typedef struct tagTRIANGLE {
VERTEX vertex[3]; } TRIANGLE;
typedef struct tagVERTEX {
float x, y, z; float u, v; } VERTEX;
If we take VERTEX as an example, why do we need to use the word "tagVERTEX" at the start of the structure definition? Surely the name of the structure is just "VERTEX" on its own?
Thanks
It's a C thing. In C, if you defined a struct as so:
In C++, you don't need to use the struct (or class) keyword like that anyway, so you can define a struct either way.
struct Vertex{ float x; float y; float z;};
And you needed to use a variable of that type, then the declaration would look like this:struct Vertex v;//Or for a function:struct Vertex Normalize(struct Vertex* pv);
Typedef-ing a "struct tagVertex" as "Vertex" means that you no longer need to bother with using the "struct" keyword everywhere.In C++, you don't need to use the struct (or class) keyword like that anyway, so you can define a struct either way.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
it's so cool when u start writing your post and then find that someone else has already answered :P
As far as I know, this is done to retain compatibility with C. In C, if you were to define a variable of type STRUCTURE1 (that is, a type defined as "struct STRUCTURE1 {};") the definition would have to include the keyword struct. More concisely, the following would have to be done:
By using the typedef, and a "dummy" name (eg. tagVERTEX) you could simply type:
STRUCTURE1 a;
to create a variable. In C++ this is not necessary, since you do not need to write "struct" in front of every definition of a variable.
edit: just a tad late...
struct STRUCTURE1 {// Declaration here};int main() {// Define variable of type STRUCTURE1struct STRUCTURE1 a;return 0;}
By using the typedef, and a "dummy" name (eg. tagVERTEX) you could simply type:
STRUCTURE1 a;
to create a variable. In C++ this is not necessary, since you do not need to write "struct" in front of every definition of a variable.
edit: just a tad late...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement