Purpose of struct, typedef struct, in C++

前端 未结 6 723
失恋的感觉
失恋的感觉 2020-12-03 05:43

In C++ it is possible to create a struct:

struct MyStruct
{
    ...
}

And also possible to do the following:

typedef struct         


        
6条回答
  •  囚心锁ツ
    2020-12-03 06:06

    There are many answers that consider both approaches as equivalent, but they are not.

    The typedef keyword is used to create a type alias, that is, it provides a new way of referring to another type. When you use typedef struct {} XXX; you are actually creating an unnamed type and then creating an alias XXX to that unnamed type. On the other hand, when you type struct XXX {}.

    Please, read the answer by Michael Burr here (which is a better answer than the one accepted for that question.

提交回复
热议问题