Custom Data Types with Enums and Structs Terminology Reference
This reference provides an overview of enumeration and struct terminology you'll encounter in this chapter. Use it as a quick lookup guide.
Program-Defined Types
| Term |
Definition |
Example |
| Program-Defined Type |
Custom type created by programmer |
enum, struct, class |
| User-Defined Type |
Another name for program-defined type |
Types you define vs built-in types |
| Fundamental Type |
Built-in C++ type |
int, double, char, bool |
| Compound Type |
Type built from other types |
Structs, classes, arrays |
Unscoped Enumerations
| Term |
Definition |
Example |
| Enumeration |
Type that defines set of named constants |
enum Color { Red, Green, Blue }; |
| Enumerator |
Named constant in enumeration |
Red, Green, Blue are enumerators |
| Unscoped Enumeration |
Traditional enum with global enumerators |
enum Color without class |
| Underlying Type |
Integer type used to store enum values |
Usually int by default |
Enum Value Conversions
| Term |
Definition |
Example |
| Integral Conversion |
Converting enum to integer |
int x = Color::Red; (implicit) |
| Explicit Conversion |
Using static_cast to convert |
static_cast<int>(myColor) |
| Implicit Conversion |
Automatic conversion by compiler |
Unscoped enums convert to int automatically |
| Enum to String |
Converting enum value to text |
Using switch or lookup table |
| String to Enum |
Parsing text to enum value |
Comparing strings to find matching enum |
Scoped Enumerations
| Term |
Definition |
Example |
| Scoped Enumeration |
Type-safe enum using enum class |
enum class Color { Red, Green, Blue }; |
| enum class |
Keyword for creating scoped enumeration |
Enumerators don't pollute outer scope |
| Scoped Enumerator |
Enumerator accessed with scope operator |
Color::Red not just Red |
| Type Safety |
Prevention of implicit conversions |
Must explicitly cast to integer |
Operator Overloading for I/O
| Term |
Definition |
Example |
| Operator Overloading |
Defining custom behavior for operators |
Custom << for printing enums |
| Stream Insertion Operator |
operator<< for output |
std::cout << myEnum; |
| Stream Extraction Operator |
operator>> for input |
std::cin >> myEnum; |
| std::ostream |
Output stream type |
Type for std::cout |
| std::istream |
Input stream type |
Type for std::cin |
Struct Basics
| Term |
Definition |
Example |
| Struct |
Type that groups related data members |
struct Point { int x; int y; }; |
| Data Member |
Variable inside struct |
x and y in Point struct |
| Member Variable |
Another name for data member |
Individual piece of struct data |
| Member Selection |
Accessing struct member with . |
point.x accesses x member |
Struct Initialization
| Term |
Definition |
Example |
| Aggregate Initialization |
Initializing struct with braces |
Point p{5, 10}; |
| Member Initializer List |
Comma-separated values in braces |
{5, 10} provides values for members |
| Default Member Initialization |
Default value in struct definition |
int x{0}; in struct definition |
| Value Initialization |
Initializing with empty braces |
Point p{}; zero-initializes members |
| Designated Initializers |
C++20 named member initialization |
Point p{.x = 5, .y = 10}; |
Struct Operations
| Term |
Definition |
Example |
| Pass by Value |
Copying entire struct to function |
void func(Point p) copies struct |
| Pass by Reference |
Passing struct reference to function |
void func(Point& p) no copy |
| Return by Value |
Returning copy of struct |
Point makePoint() returns copy |
| Struct Assignment |
Copying one struct to another |
p1 = p2; memberwise copy |
Struct Features
| Term |
Definition |
Example |
| Struct Nesting |
Defining struct inside another struct |
struct Rect { Point topLeft; Point bottomRight; }; |
| Struct Padding |
Extra bytes added for memory alignment |
Compiler may add unused bytes |
| Member Alignment |
Requirements for member memory locations |
Members aligned on boundaries |
| sizeof Struct |
Total bytes including padding |
May be larger than sum of members |
Pointers and References to Structs
| Term |
Definition |
Example |
| Member Selection Operator |
Dot operator . for direct access |
point.x |
| Arrow Operator |
-> for pointer-to-member access |
ptr->x same as (*ptr).x |
| Pointer to Struct |
Pointer holding address of struct |
Point* ptr = &point; |
| Reference to Struct |
Reference to struct object |
Point& ref = point; |
Class Templates
| Term |
Definition |
Example |
| Class Template |
Blueprint for creating generic types |
template<typename T> struct Pair |
| Template Class |
Specific instantiation of template |
Pair<int> is template class |
| Template Argument |
Type used to instantiate template |
int in Pair<int> |
| Generic Programming |
Writing code that works with any type |
Using templates for type flexibility |
Template Argument Deduction
| Term |
Definition |
Example |
| CTAD |
Class Template Argument Deduction |
std::pair p{1, 2.5}; deduces types |
| Deduction Guide |
Rule helping compiler deduce template args |
Custom guide for complex deduction |
| Implicit Deduction |
Automatic type inference from constructor |
std::vector v{1, 2, 3}; deduces int |
| Explicit Template Arguments |
Manually specifying template types |
std::pair<int, double>{1, 2.5} |
Type Aliases
| Term |
Definition |
Example |
| Type Alias |
Alternative name for a type |
using Distance = double; |
| Alias Template |
Template that creates type aliases |
template<typename T> using Ptr = T*; |
| typedef |
Old-style type alias keyword |
typedef double Distance; |
| Template Alias Instantiation |
Using alias template with specific type |
Ptr<int> becomes int* |