Dark Bit Factory & Gravity
PROGRAMMING => C / C++ /C# => Topic started by: James_wright_1990 on December 16, 2009
-
I'm in the processes of moving alot of my code across to C++ and i have been using the nehe tutorials to help with coding some of my c# opengl stuff int C++ and i have come across something called typedef. Up until now i have been ok because alot of c++ is similare to C# but this one confuses me. I usually just go back to my lecture notes but it doesnt seem to be in thereand the stuff i found on the internet is a little confusing is see code like:
typedef struct
{
//code
}
nameofstuct
what is the difference between that and:
struct nameofstruct
{
//code
}
if anyone can shed somelight on this i would greatly appreaciat it.
James
-
In C++ struct and class are very similar, and similar to the same things in C#.
In C++ typedef is a way of creating an alias for a struct - it can't have any code in it.
So you can do
typedef struct
{
double real;
double imaginary;
} complex_number;
and then you can use
complex_number z;
to declare one.
This saves you having to write
struct
{
double real;
double imaginary;
} z;
I'm not sure if having 'class' in the language means that typedef is redundant, I'm more of a C programmer :)
Jim
-
Ah thanks for that that makes much more sense now i was getting confused by the examples but i think i get it now. I'm more used to using classes so i think i'll stick with that for now but its good to know what it means when i see it in the examples.
Cheers for the help.
James
-
typedef struct
{
//code
}
nameofstuct
what is the difference between that and:
struct nameofstruct
{
//code
}
In C++ the "typedef" is redundant but is often used for compatibility reasons because in C (not C++) the "struct" keyword is required for every instantiation:
struct Vector3D {
float x,y,z;
};
struct Vector3D v;
As this is quite annoying you can create an alias using "typedef":
typedef struct Vector3D Vector;So now you can simply write:
Vector v;Actually we aren't interested in "struct Vector3D" anymore, so we don't even supply a name for it:
typedef struct{float x,y,z;} Vector;
-
In C++ the "typedef" is redundant but is often used for compatibility reasons because in C the "struct" keyword is required for every instantiation:
struct Vector3D {
float x,y,z;
};
struct Vector3D v;
As this is quite annoying you can create an alias using "typedef":
typedef struct Vector3D Vector;So now you can simply write:
Vector v;Actually we aren't interested in "struct Vector3D" anymore, so we don't even supply a name for it:
typedef struct{float x,y,z;} Vector;
ah i see that deos make the code look a lot nicer. My lecturers this year seem to not like structs (whish i can undersatnd) so i dont use them very often but i do use them occasionally and I'll definately use typedef now i know what it does
thanks
James
-
In C++ "struct" has exactly the same features as "class".
The only difference is that by default "struct" is public and "class" is private.
"class" sounds more oop, though.
"typedef" becomes rather useful when handling nested templates and types start to look like this:
std::map< std::pair<std::string, std::string>, std::vector<std::string> >
-
Ah right so in C++ is there not as clear a distinction between a class and a struct. Im used to C# where there is a deffinate difference. if there isn't a difference why do my lecturers seem to dislike structs so much?
James
-
-
-
As for classes and structs; the same difference that exists in C# applies here. That is structs only hold data, whereas classes can also have 'behaviour' (functions/methods).
No, in C++ "struct" can make use of the whole oop stuff.