Skip to main content
gamedev.net
🔒 Locked

Typedef?

Started by doogle 7 replies 300+ views
Original Post
doogle
doogle
Can someone fill me in on what typedef is and why you would use it? So far a gather from the name thats its a type definition, but i dont understand the difference between it and normal class definitions!
Jeff D
Jeff D
Basically its like renaming something

a good example IMO is

typedef unsigned short USHORT;

everytime I want to make an unsigned short I could write this:

USHORT shortInt;

instead of:

unsigned short shortInt;

Basically to my knowledge( which isn''t much ) thats what it used for.

Jeff D


Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
simbiant
simbiant
then what is the point of it?

you could easily just do

define USHORT unsigned short

USHORT temp;
Null and Void
Null and Void
Simbiant:
1) Defines are more ''dangerous'' than typedefs.
2) You can do more complex things with typedefs:
  
typedef int (*MyFuncPointer)(int, char *, double *);

MyFuncPointer Thingy;

/* The equivelant macro: */
#define MyFuncPointer(name) int (*name)(int, char *, double *)

MyFuncPointer(Thingy);

I think I prefer the first one. I don''t know about you, but I prefer the safer, more versatile, typedef solution to the macro solution in every situation possible .

[Resist Windows XP''s Invasive Production Activation Technology!]
doogle
doogle
but what about something like this:

typedef struct {
int x;
int y;
int z;
} TVertex;



now this might not be quite right, but i have seen something like this before. Why do you make it a typedef, not just a struct?
Oluseyi
Oluseyi
quote:
Original post by doogle
Why do you make it a typedef, not just a struct?

Older versions of C wouldn''t allow you to declare an instance of that structure without the use of the struct keyword unless you had typedef (making the tag an alias for the struct).

It''s deprecated, so don''t worry about it.
zipless
zipless
I''m pretty sure that typedef''s aren''t part of C++ and as a result all structs are automatically typedefs (or something like that).

/ Ignorance is bliss, then you go and spoil it by learning stuff /
Oluseyi
Oluseyi
quote:
Original post by zipless
I''m pretty sure that typedef''s aren''t part of C++...

Wrong. All structs are typedef in a sense, since structs are classes with default public visibility, but typedefs do still exist and are useful. take a look at all the non-struct related examples in this thread alone.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.