Could anybody please tell me what is the main difference between C & C++ structures.
C++ structures behave like C++ classes, allowing methods, contructors, destructors... The main diffrent between classes and C++ structures is that structures by default are everything inside public, while classes by default everything inside is private (ie: nothing outside can access them directly)
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.
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.
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.