- Composite type
In
computer science , composite types aredatatype s which can be constructed in aprogramming language out of that language'sprimitive types and other composite types. The act of constructing a composite type is known as composition.C/C++ structures and classes
A
struct
is C's andC++ 's notion of a composite type, a datatype that composes a fixed set of labelled fields or members. It is so called because of thestruct
keyword used in declaring them, which is short for "structure" or, more precisely, "user-defined data structure".In C++, the only difference between a
struct
and a class is the defaultaccess level , which is "private" for classes and "public" forstruct
s.Note that while classes and the
class
keyword were completely new in C++, the C programming language already had a crude type ofstruct
s. For all intents and purposes, C++struct
s form asuperset of Cstruct
s: virtually all valid Cstruct
s are valid C++struct
s with the same semantics.Declaration
A
struct
declaration consists of a list of fields, each of which can have any type. The total storage required for astruct
object is the sum of the storage requirements of all the fields, plus any internal padding.For example:
defines a type, referred to as
struct Account
. To create a new variable of this type, we can writestruct Account myAccount;
which has an integer component, accessed bymyAccount.account_number
, and a floating-point component, accessed bymyAccount.balance
, as well as thefirst_name
andlast_name
components. The structuremyAccount
contains all four values, and all four fields may be changed independently.Since writing
struct Account
repeatedly in code becomes cumbersome, it is not unusual to see atypedef
statement in C code to provide a more convenient synonym for thestruct
.For example:
In C++ code, the
typedef
is not needed because types defined usingstruct
are already part of the regular namespace, so the type can be referred to as eitherstruct Account
or simplyAccount
.As another example, a three-dimensional Vector composite type that uses the floating point data type could be created with:
A variable named
velocity
with aVector
composite type would be declared asVector velocity;
Members of thevelocity
would be accessed using a dot notation. For example,velocity.x = 5;
would set thex
component ofvelocity
equal to 5.Likewise, a color structure could be created using:
In 3D graphics, you usually must keep track of both the position and color of each vertex. One way to do this would be to create a
Vertex
composite type, using the previously createdVector
andColor
composite types:Instantiation
Create a variable of type
Vertex
using the same format as before:Vertex v;
Member access
Assign values to the components of
v
like so:
=Primitive Subtyping=The primary use of
struct
is for the construction of complex datatypes, but sometimes it is used to circumvent standard C conventions to create primitive subtyping. For example, common Internet protocols rely on the fact that C compilers insert padding between struct fields in predictable ways; thus the codewill work correctly.
Function types
Function type s (ortype signature s) are constructed from primitive and composite types, and can serve as types themselves when constructing composite types:ee also
*
Object composition
Wikimedia Foundation. 2010.