Why does C need “struct” keyword and not C++?

后端 未结 6 809
臣服心动
臣服心动 2020-11-27 03:38

I\'ve always been a little confused about what\'s going on here:

#include 

int main() {  
    timeval tv;
    tv.tv_sec = 1;

    for (;;) {
         


        
6条回答
  •  星月不相逢
    2020-11-27 04:25

    I would say it's a design decision of both languages.

    Structs in C are just structured records and have different usage then built-in type.

    C++ has ctors and operator overloads and so they act as types.

    struct foo x;         // create a structure of pattern foo
    typedef foo foo_type; // "define" a type
    foo_type x;           // create an instance of type foo_type
    

    C++:

    foo x; // create an instance of type foo
    

    As a side-note, struct foo is still allowed in C++. struct foo is easier to parse then typedef'dfoo as the name-lookup is simpler.

提交回复
热议问题