C structure and C++ structure

后端 未结 6 876
挽巷
挽巷 2020-12-13 04:35

Could anybody please tell me what is the main difference between C & C++ structures.

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 04:57

    There are several differences in C and C++ structure

    1. In C we define struct keyword is necessary to create the structure type value while in C++ it is not necessary.

    2. In C there is no function inside the structure while in C++ we can define function that can access the data members of structure directly( Function is names as method in C++ )

    3. There is no concept of access modifier inside the structure in C while in C++ we can find the access modifier (eg. private and public ). By default all are public.

    4. Structure in C can not have static members while in C++ structure can have static members.

    5. Size of empty structure is constraint violation in C, but it is always 1 in C++.

    6. We can have both pointers and references to struct in C++, but only pointers to structs are allowed. (References aren't feature of C language)

提交回复
热议问题