Structures
The C programming language has structs (and unions). C++ offers structs and classes (and unions). Here’s a bit of an overview of these constructs.
C
Structs
C’s structs are quite different from C++ structs, they are more limited.
Some key points:
- only data members can be used, no methods!
- they can have no constructors or destructors
- they cannot have access modifiers, hence no encapsulation
- they may be referred to by pointer only, no references
That’s basically it, structs are a way to group related data (not functionality) together.
Typedef
Sometimes, you can find structs defined using the typedef
keyword, like this:
It’s basically the same thing as the previous example. typedef
is used to apply a custom name to any type, e.g.:
Unions
Unions allow us to specify a structure with multiple members, where only one of those members is valid at a time. Any member of a union is stored in the same location in memory, so setting any of the members actually overwrites the previous member that was set.
The purpose of this structure type is to save memory on systems where that resource is very limited (embedded systems).
Here’s a small example:
As you can see, only the some_string
member has a valid value. The integer
got messed up, because it’s reading the same memory location where a “Whatever”
was written, overwriting the original value - 24
.
C++
C++ is an Object-Oriented Programming language (although it could be considered multi-paradigm). It has both structs and classes. There is just one difference between them:
Structs have their members public by default, while classes have their members private by default.
Other than that, structs and classes are pretty much the same. Some features:
- they can hold data
- they can have methods
- they can have constructors/destructors
- they may be referred to by pointer or a reference
- they support access modifiers
You can find opinions that a class
is supposed to be used to hold data
and functionality, while a struct
is for related data, similar to C.
I think that in the ideal world, there would be just one - either a class or a struct. I think the only reason to have both is:
- Structs, with their syntax would be kept compatible with C (assuming we don’t use methods, and other non-C things) where everything is public.
- Classes are supposed to be the “ideal” OOP construct, introduced for those who want to write new programs in an OOP style.