020 C++的结构体(struct)


作者Lou Xiao, deepseek创建时间2025-04-02 15:17:56更新时间2025-04-02 15:17:56

1. struct基本概念

1.1 定义与声明

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Person {
2 // 成员变量
3 std::string name;
4 int age;
5
6 // 成员函数
7 void introduce() {
8 std::cout << "Name: " << name << ", Age: " << age << std::endl;
9 }
10 };

1.2 structclass的区别

特性structclass
默认访问权限publicprivate
继承默认权限publicprivate
使用习惯数据聚合面向对象

2. struct的成员

2.1 数据成员

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Point {
2 double x; // 公有数据成员
3 double y;
4 };

2.2 成员函数

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Rectangle {
2 double width;
3 double height;
4
5 // 成员函数
6 double area() {
7 return width * height;
8 }
9 };

2.3 静态成员

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Counter {
2 static int count; // 静态成员变量
3
4 Counter() { count++; }
5 static void showCount() { // 静态成员函数
6 std::cout << "Count: " << count << std::endl;
7 }
8 };
9 int Counter::count = 0; // 静态成员初始化

3. struct的高级特性

3.1 构造函数与析构函数

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Student {
2 std::string name;
3 int grade;
4
5 // 默认构造函数
6 Student() : name("Unknown"), grade(0) {}
7
8 // 参数化构造函数
9 Student(const std::string& n, int g) : name(n), grade(g) {}
10
11 // 析构函数
12 ~Student() {
13 std::cout << "Student " << name << " destroyed" << std::endl;
14 }
15 };

3.2 运算符重载

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Complex {
2 double real;
3 double imag;
4
5 Complex operator+(const Complex& other) {
6 return {real + other.real, imag + other.imag};
7 }
8 };

3.3 友元函数

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Box {
2 private:
3 double width;
4
5 public:
6 friend void printWidth(const Box& box); // 友元函数声明
7 };
8
9 void printWidth(const Box& box) {
10 std::cout << "Width: " << box.width << std::endl;
11 }

4. struct的继承与多态

4.1 继承

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Base {
2 int baseData;
3 void baseFunc() { /*...*/ }
4 };
5
6 struct Derived : Base { // 默认public继承
7 int derivedData;
8 void derivedFunc() { /*...*/ }
9 };

4.2 多态

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Shape {
2 virtual void draw() { // 虚函数
3 std::cout << "Drawing a shape" << std::endl;
4 }
5 virtual ~Shape() {} // 虚析构函数
6 };
7
8 struct Circle : Shape {
9 void draw() override {
10 std::cout << "Drawing a circle" << std::endl;
11 }
12 };

5. struct的特殊用法

5.1 POD (Plain Old Data) 类型

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct POD_Example {
2 int x;
3 double y;
4 char z[10];
5 };
6 // 是POD类型,适合与C语言交互

5.2 位域

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct BitField {
2 unsigned int flag1 : 1; // 1位
3 unsigned int flag2 : 3; // 3位
4 unsigned int flag3 : 4; // 4位
5 };

5.3 匿名struct

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct {
2 int x;
3 int y;
4 } point; // 直接创建point实例

6. 现代C++中的struct

6.1 结构化绑定 (C++17)

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Coordinate {
2 int x;
3 int y;
4 int z;
5 };
6
7 Coordinate c{1, 2, 3};
8 auto [x, y, z] = c; // 结构化绑定

6.2 用户定义推导指南 (C++17)

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Point {
2 double x, y;
3 Point(double a, double b) : x(a), y(b) {}
4 };
5
6 // 用户定义推导指南
7 Point(double, double) -> Point;

6.3 三向比较运算符 (C++20)

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 struct Book {
2 std::string title;
3 int year;
4
5 auto operator<=>(const Book&) const = default;
6 };

7. 最佳实践

  1. 使用struct作为数据聚合体,class作为面向对象实现
  2. 对于简单的数据容器,优先使用struct
  3. 需要与C语言交互时,使用POD struct
  4. 考虑将相关操作作为成员函数加入struct
  5. 对于小型、频繁创建的对象,struct通常比class更合适

8. 示例代码

1.双击鼠标左键复制此行;2.单击复制所有代码。
                                
                                    
1 #include <iostream>
2 #include <string>
3 #include <compare>
4
5 // 基本struct示例
6 struct Employee {
7 std::string name;
8 int id;
9 double salary;
10
11 void display() const {
12 std::cout << "ID: " << id << ", Name: " << name
13 << ", Salary: " << salary << std::endl;
14 }
15 };
16
17 // 带构造函数的struct
18 struct Date {
19 int day, month, year;
20
21 Date(int d, int m, int y) : day(d), month(m), year(y) {}
22
23 void print() const {
24 std::cout << day << "/" << month << "/" << year << std::endl;
25 }
26 };
27
28 // 现代C++特性
29 struct Product {
30 std::string name;
31 double price;
32 int stock;
33
34 // C++20三向比较
35 auto operator<=>(const Product&) const = default;
36 };
37
38 int main() {
39 Employee emp1{"John Doe", 1001, 50000.0};
40 emp1.display();
41
42 Date today{15, 6, 2023};
43 today.print();
44
45 Product p1{"Laptop", 999.99, 10};
46 Product p2{"Phone", 699.99, 15};
47
48 if (p1 < p2) {
49 std::cout << p1.name << " is cheaper than " << p2.name << std::endl;
50 }
51
52 return 0;
53 }