Click to See Complete Forum and Search --> : purpose of typedef struct etc. ?


Remington
09-01-2003, 11:58 PM
I see this kind of thing a lot:

typedef struct
{
//
} name;

or this

typedef enum
{
//
} name;

My question is why do people do this? Does this notation have some kind of special function? Isn't it simpler to write:

struct name
{
//
};

and

enum name
{
//
};

????
Might it have something to do with C vs. C++ if so why do I see this type of notation in purely c++ files which have c++ only code all over the place?
I have never come across an instance where I would even concieve of using the previous method so if somone would be so kind and explain what the point of that is I would be grateful.

kid A
09-02-2003, 08:25 AM
I dont recognise the language, but it seems the variable type is being defined. Sometimes it is necessary to set the datatype for a variable manually, even if it (like in PHP) is set automatically. You might want to override the auto datatype.

EverlastingGod
09-02-2003, 08:57 AM
typedef struct
{
// ...
} name;

// means you can declare variables as
name myVar;

struct name
{
// ...
};

// means you have to declare variables as
struct name myVar;

Remington
09-02-2003, 01:15 PM
No the use of defined types is exactly the same as far as I can tell.
Note this is C++.

typedef struct
{
//
} name;

//and

struct name
{
//
};

//can both be delcared as, at least on my compiler msvc .net

name myVar;


The usage of these types is exactly the same, so I'm still left wondering what the point of all that is. My best guess is that it's just programming preference, but if that's true then why is it overwhelmingly popular when it is so obviously more confusing to read/write? It even messes up my classview in msvc, I end up with a whole lot of unnamed classes/structs and a whack of needless typedefs so I have to poke through each one looking for whatever members I need, thus completely negating the purpose of a class viewer! (whew)

EverlastingGod
09-02-2003, 01:39 PM
Oh, I meant C.

Yeah, they do the same thing in C++. Just C-style carried over to C++.

gameboy1234
09-02-2003, 04:25 PM
Originally posted by Remington
No the use of defined types is exactly the same as far as I can tell.
Note this is C++.

typedef struct
{
//
} name;

//and

struct name
{
//
};

//can both be delcared as, at least on my compiler msvc .net

name myVar;


The usage of these types is exactly the same, so I'm still left wondering what the point of all that is. My best guess is that it's just programming preference, but if that's true then why is it overwhelmingly popular when it is so obviously more confusing to read/write? It even messes up my classview in msvc, I end up with a whole lot of unnamed classes/structs and a whack of needless typedefs so I have to poke through each one looking for whatever members I need, thus completely negating the purpose of a class viewer! (whew)

typedefs are required for structs in C (when delcaring a new type). C++ automatically declares a new type when a struct is declared so typedef is not needed for C++ structs.

What you're probably seeing is not preference but C code. If it's C++, then the programmer was probably a C programmer who didn't know that typedef was redundant for C++.

I'd suggest the problems you are seeing in MSVC are problems in MSVC.