Talk:Typedef
Can anyone tell me what typedef uint32 A; would do ?
It would create a new type named 'A' that would essentially just be an alias for uint32. You could then create variables like this: 'A newvariables = 3;' Jaddle 17:06, 14 May 2007 (UTC)
It would be nice to have an explanation of the syntax of recursive typedefs
For example, in C:
int main() {
typedef struct { int key; struct MyType* next; } MyType;
MyType a; MyType b;
MyType* aptr; MyType* bptr;
aptr = &a; bptr = &b;
aptr->key = 3; aptr->next = bptr;
}
The word "struct" is required before the phrase "mytype* next;" The GNU C compiler (3.4.6) warns that "aptr->next = bptr;" is an assignment of incompatible pointer types. —Preceding unsigned comment added by 155.148.36.138 (talk) 14:31, 6 September 2007 (UTC)
Note that the example given in the page is an excellent example of everything that can go wrong with typedefs. First, your typedef is usually in a separate file, so all the coder will see is:
apple coxes;
Now, ok, what can I do with coxes that are of type apple? Eat them? No I can increment them:
coxes += 1
So what exactly happens when you increment an apple? Especially in C++ with type overloading? All you've done is obscure the type of the variable. The data type should specify the variables _data_ _type_. Not describe it. That's what the name is for. The following code would be much, much clearer:
int coxe_apple_cnt = 0;
Now we have a variable that is a count of the number of coxe apples (coxe is a type of apple, I presume). It's type is int. Now we have a much clearer notion of what:
coxe_apple_cnt++;
Does.
So what's good about typedefs again?