C structure and C++ structure

C++C

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

In C++ struct and class are the exact same thing, except for that struct defaults to public visibility and class defaults to private visiblity.

In C, struct names are in their own namespace, so if you have struct Foo {};, you need to write struct Foo foo; to create a variable of that type, while in C++ you can write just Foo foo;, albeit the C style is also permitted. C programmers usually use typedef struct {} Foo; to allow the C++ syntax for variable definitions.

The C programming language also does not support visibility restrictions, member functions or inheritance.

Solution 2 - C++

In C++, structures behave like classes, allowing methods, constructors, destructors etc...

The main difference between classes and C++ structures is that everything inside structures is public by default, while everything inside classes is private by default. (ie: nothing outside can access them directly)

Solution 3 - C++

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)

Solution 4 - C++

C structs is more akin to a definition of a composite data structure

C++ structs can be thought of as a class but scope of all member variables are defaulted to public.

Solution 5 - C++

In addition to the answers above, remember that C++ structures support inheritance and so can contain pointers to vtables. This can make a big difference when serializing and deserializing these structures across processes. Templates are supported too.

Solution 6 - C++

C : we can't define function inside the structure in c.

C++ : We can define function inside the structure in c++.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVijayView Question on Stackoverflow
Solution 1 - C++TronicView Answer on Stackoverflow
Solution 2 - C++speederView Answer on Stackoverflow
Solution 3 - C++Sandeep_blackView Answer on Stackoverflow
Solution 4 - C++Anthony KongView Answer on Stackoverflow
Solution 5 - C++Agnel KurianView Answer on Stackoverflow
Solution 6 - C++Herat PatelView Answer on Stackoverflow