devxlogo

Avoid Typedefs When Defining Structs

Avoid Typedefs When Defining Structs

In olden days, before C++ appeared, it was customary to declare a struct as a typedef name. For example:

   typedef struct DATE_TAG  {    int day;    int month;    int year;  } Date;  /* 'Date' is a typedef */

This way, one could create an instance of the struct without having to use the keyword ‘struct’:

      /* C code */  Date date; /* typedef; 'struct' not required */  struct DATE_TAG another_date; /* 'struct' is required */

In C++, the use of a typedef in this context is unnecessary because you don’t need the elaborated type specifier (i.e., struct, union, and class) to create an instance:

     // C++ code  DATE_TAG another_date; // 'struct' not required in C++

Therefore, you shouldn’t declare structs as typedef names anymore.

devx-admin

Share the Post: