Purpose of struct, typedef struct, in C++

前端 未结 6 696
失恋的感觉
失恋的感觉 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:23

    You would use a typedef so you do not need to specify the struct keyword when declaring variables of that struct.

    Without typedef:

    struct MyStruct foo;
    

    With typedef:

    MyStruct foo;
    

    Basically you are making MyStruct appear as a new type so it also implements some level of type abstraction as programmers do not need to know explicitly what type it is. You can pass MyStruct into a function, manipulate the data in it and return it and the programmer need never worry about what actually happens.

    Much of the C standard library uses typedefs for this reason.

提交回复
热议问题