Purpose of struct, typedef struct, in C++

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

    The typedef version is a special case of

    typedef foo bar;
    

    which defines a new "type" bar as an alias for foo. In your case, foo happens to be a struct. In C, this was the only way to introduce new "types" (in quotes, because they are not really equivalent to int, float and co). In C++, this is not so useful, because C++ was designed to make definition of new types easier and more complete than C (at least at the beginnings of C++), and the typedef is not even necessary to refer to a previously declared struct (or class).

提交回复
热议问题