Difference between 'struct' and 'typedef struct' in C++ program?

C++Server Side ProgrammingProgramming

Basically struct is used to define a structure. But when we want to use it we have to use the struct keyword in C. If we use the typedef keyword, then a new name, we can use the struct by that name, without writing the struct keyword.

In C++, there is no difference between 'struct' and 'typedef struct' because, in C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name.

Though there is one subtle difference that typedefs cannot be forward declared. So for the typedef option, you must include the file containing the typedef before it is used anywhere.

raja
Published on 28-Mar-2019 17:57:13
Advertisements