Jump to content
  • Advertisement
Sign in to follow this  
Crusable77

Typedef

This topic is 1842 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

What exactly does typedef do? if I have an enum like so:

 

typedef enum STATE{MENU = 0, PLAY = 1, CREDITS = 2} m_CurrentState; 

 

vs

 

enum STATE{MENU = 0, PLAY = 1, CREDITS = 2} m_CurrentState;

 

what is the difference?

Edited by Crusable

Share this post


Link to post
Share on other sites
Advertisement

If you don't use typedef in C, then every time you want to declare a variable of that type you need to use enum. In C++ it was made so that named enumerations (structures and unions too) are typedef'd implicitly.

 

For example:

 

enum Colors {
   RED,
   GREEN,
   BLUE
};
 
enum Colors var1; // Works in C and C++
Colors var2;      // Works in C++ only
Edited by Sik_the_hedgehog

Share this post


Link to post
Share on other sites

agreed...
 
the typical convention is something like:

typedef struct Foo_s Foo;
typedef struct Bar_s Bar;

struct Foo_s {
...
};

struct Bar_s {
...
};

 
which generally has the property of working in both C and C++.

this also allows each struct to contain members using the typedef'ed type (or at least, pointers to the type, non-pointer members have a few more restrictions here...).

 


in contrast:

typedef struct {
...
}Foo;

will only allow Foo to be referenced by things declared after it.

 

 


but, yeah, there are a lot of "little differences" like this between the languages.

 

there are also cases where the languages diverged, such as the treatment of 'void *' and some behavior related to implicit conversions, as well as differences in terms of language features.
 
then, there are all the C++ features which don't exist in C (classes, namespaces, templates, ...), and sometimes different conventions for addressing similar types of problems.
 
but, yeah, for cases where it matters, it is generally possible to write code for the common subset between C and C++, though more often code is written intended to be one language or the other. though, sometimes, it is useful (such as when porting code between C and C++).

Share this post


Link to post
Share on other sites
Hello,
to answer the original question:
typedef enum STATE{MENU = 0, PLAY = 1, CREDITS = 2} m_CurrentState;
Creates two type names: enum STATE (in C++ also available simply by STATE) and m_CurrentState. Note that m_CurrentState is a type name in this case, which makes the m_ prefix very misleading.
 
enum STATE{MENU = 0, PLAY = 1, CREDITS = 2} m_CurrentState;
Creates enum STATE type and declares a variable m_CurrentState of enum STATE type.

Share this post


Link to post
Share on other sites
To further clarify: in C there were two namespace, the typedef namespace and the regular namespace. Putting a name into the typedef namespace allowed you to apply a clearer naming scheme to certain types: a sort of crude subclassing twenty tears before OO was invented. Because it was a separate namespace, it allowed you to use shortcuts so you did not have to type 'struct' and 'enum' for every instance, and still not have name collisions.

When typedefs were introduced, struct member names were also in the global namespace. That's why you often see old code using a common prefix for struct member names eg. (from Unix):
 struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!