struct and typedef in C versus C++

后端 未结 3 637
说谎
说谎 2020-12-06 07:02

I am currently using a C++ IDE for something that will need to work on C, and wanted to make sure that I won\'t have problems with this later on. After making the struct bel

3条回答
  •  一向
    一向 (楼主)
    2020-12-06 07:15

    typedef struct THIS_IS_A_TAG
    {
        int a;
        int b;
    } THIS_IS_A_TYPEDEF;
    
    THIS_IS_A_TYPEDEF object1;     // declare an object.       C:Ok,     C++:Ok
    struct THIS_IS_A_TAG object2;  // declare another object.  C:Ok,     C++:Ok
    THIS_IS_A_TAG object3;         // declare another object.  C:Not Ok, C++:Ok
    

    The reason for the typedef is because C programmers would like to be able to do that third thing, but they can't.

提交回复
热议问题