Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Oct 14, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date