Archived

This topic is now archived and is closed to further replies.

stupid newbie question about classes/structures w/ typedef

This topic is 5050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

high, im using c++ with visual c++ 6. i just have a simple question. what is the difference between

typedef struct
{

int blah;
int blah2;
int etc;

}MYSTRUCT;

and


struct MYSTRUCT
{

int blah;
int blah2;
int etc;

};

??? so what is the difference? maybe its because i dont understand what typedef means. i thought typedef was used to just redefine built in data-types, such as typedef int integer; typedef BLAH double; now we could use integer instead of int, and BLAH instead of double, like integer x = 5; BLAH y = 3.52; so could someone please explain the difference between these 2? and what typedef does? thank you for any help you can give me, i appreciate it!!!! [edited by - graveyard filla on February 19, 2004 12:50:26 AM]

Share this post


Link to post
Share on other sites
Since you are using C++, there is no difference. I think in C (I never learned it), the only way to create objects of a struct was to either declare them right after the struct definition, or make the struct a data type with a typedef and make objects later. It didn't make the struct a type automatically.

I am not sure about that, though, but I am sure that in C++, those are the same.

EDIT: Wow, what I wrote is impossible to read, but I am too tired to reword it.

[edited by - PlayGGY on February 19, 2004 12:58:59 AM]

Share this post


Link to post
Share on other sites
In C, the name of a struct is not a fully qualified type, so you would have to declare your variables in a manner like "struct mystruct x;". In order to save themselves the trouble of writing "struct" all over the place, C programmers therefore typedef the entire thing to a single type name. In C++, this is not necessary; the name of a struct or class is a fully qualified type name by itself.

On typedefs: Yes, you have the right idea. Their true power is in abstracting away underlying primitives (see OpenGL, for instance: Since they require a certain precision for their internal floating-point variables, they declare their own type GLfloat which can be typedefed to the appropriate primitive, which may be float, double, long double, or something else, depending on your platform), and for the sake of convenience when dealing with extremely long type names (I use it all the time with templated classes, especially when my classes are templated on multiple types or are nested templates).

[edited by - Miserable on February 20, 2004 1:03:14 AM]

Share this post


Link to post
Share on other sites
In C you want to write:

typedef struct X
{
struct X *pX;
}X;

then you can do:

void foo(X *x);

instead of:

void foo(struct X *x);

the first visible ''X'' id name will allow you to make self referential structs the last ''X'' makes is so you don''t have to type ''struct'' everywhere.

Share this post


Link to post
Share on other sites