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.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist