-
Struct equivilence
what would be the most likely equivilence for a struct? C programmers LOVE structs, but as a noob C++ programmer, I'm not well versed with them.
EX:
Code:
typedef struct{
float x, y;
float z;
float r, g, b;
} Vert;
typedef struct{
int n;
Vert *verts;
}Poly;
Would a class replace a struct? If a class wasn't necessarily the best way to go, is there another way to do this without using struct?
thanks for not flaming me too bad :)
tms
-
Well ... yeah. Why not.
But if you want to look into classes you'll need to understand scructs really. Just cause C++ uses classes it doesn't mean you don't use structs. If you want something to simply hold data use a struct, but if you need to use a lot of functions along with the data try a class.
-
There are structs in C++. They are the same as classes, except they default to public access instead of private. Unlike in C, they do not require the typedef to be used as types, so the equivalent in C++ could be:
Code:
struct Vert {
float x, y;
float z;
float r, g, b;
};
struct Poly {
int n;
Vert *verts;
};
or:
Code:
class Vert {
public:
float x, y;
float z;
float r, g, b;
};
class Poly {
public:
int n;
Vert *verts;
};
-
ok, then how do I access the structs? Is it customary to put the structs in a different file or above main?
-
For large projects, the structure or class definitions would certainly be in separate files. For a small project, you could put them in the same file if you wanted to.
If you need to access those structures in multiple source files, then the structure needs to be in a header file so that you can include it in multiple source files.
-