| Composite Type |
Article Index for Composite |
Website Links For Composite |
Information AboutComposite Type |
| CATEGORIES ABOUT COMPOSITE TYPE | |
| data structures | |
|
C/C++ A struct is C 's and C++ 's notion of a composite type, a datatype that composes a fixed set of labelled fields or '''members'''. It is so called because of the struct 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 default Access Level , which is ''private'' for classes and ''public'' for structs.Note that while classes and the class Keyword were completely new in C++, the C Programming Language already had a crude type of structs. For all intents and purposes, C++ structs form a Superset of C structs: virtually all valid C structs are valid C++ structs with the same semantics.A struct declaration consists of a list of fields, each of which can have any type. The total storage required for a struct 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 write struct Account myAccount;which has an integer component, accessed by myAccount.account_number, and a floating-point component, accessed by myAccount.balance, as well as the first_name and last_name components. The structure myAccount 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 a typedef statement in C code to provide a more convenient synonym for the struct. For example:In C++ code, the typedef is not needed because types defined using struct are already part of the regular namespace. So the previous example could be written (in C++) asAs another example, a three-dimensional Vector composite type that uses the floating point data type could be created with: A variable named velocity with a Vector composite type would be declared as Vector velocity; Members of the velocity would be accessed using a dot notation. For example, velocity.x = 5; would set the x component of velocity 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 created Vector and Color composite types:Create a variable of type Vertex using the same format as before: Vertex v;Assign values to the components of v like so:PRIMITIVE SUBTYPING The primary use of struct is for the construction of complexdatatypes, 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 code is often assumed to work as expected if the operate_on_ifoo function only accesses fields x and y of its argument.In C++, this can be more safely expressed as: SEE ALSO |
|
|