+4  A: 

I think that's the same as in C, typedef creates an alias of a type... in your first case, the name of the type is "struct Person", while in the second case is just "Person".

Usually, when you have to declare self referencing structures (like lists), you use both, because the typedef has not effect until the structure is defined (unless you make a forward declaration), for example:

typedef struct node {
    void *data;
    struct node *next;
} TNode, *PTNode;

so now you can declare variables of the same type in the following ways:

struct node *node1;
TNode *node2;
PTNode node3;

the three variables above are the same, pointers to the node structure.

fortran
This was the understanding I got from the wikipedia page.
Anthony D
Then you got it right :-)
fortran
In C++ (which is what the question was about), this is not the same as in C.
sbi
@sbi I cannot see how it differs from C to C++... maybe you'll be kind to explain it to us.
fortran
@fortran: See my answer. In C++, `struct X {}` can be referred to as `X` (while in C, it must be referred to as `struct X`). So in C++, there is no need for the `typedef` at all. Except when you write headers that are supposed to be parsed by a C compiler, too, this is just a C-ism that people use without thinking (or knowing) about.
sbi
+2  A: 

Usage of typedefs for structs in C is explained here. (in C++, structs are just classes with members that are public by default, whereas class members are private by default)

KSchmidt
Ahh... good call that didn't come up when I searched, thanks!
Anthony D
Actually, the question you linked to was about C, while Anthony asked about C++. And there is a difference.
sbi
The way the question was phrased, it looked like there was confusion about the use of typedefs in C as well. I'll revise my answer.
KSchmidt
A: 

c-compatibility essentially same in c++, there for legacy c-code

not sure about referring to static functions and typedefs in the unnamed struct when you do the definition this way - I never do.

+1  A: 

In C, structs live in their own name space, so you have to write struct Person if you want to use the struct's type name. The typedef eliminates the need for that prefix.

In C++, structs live in the same name space as everything else, so there's no need to do this. It's usually seen as an unnecessary C-ism.

sbi
A: 

you can tell the difference when passing Person or struct Person to a function

If you defined

struct Person { ... };

You should pass it to a function explicitly stating that it is a struct

void foo(struct Person person) { ... }

On the other hand, if you defined

typedef struct Person { ... };

You must not state that it is a struct, since the keyword typedef allows you to actually define a data type, in this case Person and you pass it this way

void foo(Person person) { ... }

pretty much like any other data type

Hope this helps

nairdaen
You meant typedef struct { ... } Person;right? Anyway, this is only so in C, not in C++.
sbi
A: 

In C++ typedefs are synonyms for other types rather than distinct types. They are generally used for two purposes:

  • Creating portable code where you abstract the platform specific away. On one platform you can have typedef int int32 and on another platform typedef long int32. You would use int32 throughout your code and when porting to the a new platform you would only have to modify the typedefs to match your requirements.

  • Creating aliases for very complicated types. E.g. typedef char const* const* cppchar. Instead of writing char const* const* in your code you can now write cppchar.

Martin Liversage