8 结构体
8.1 定义与使用
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:struct 结构体名 { 结构体成员列表 };
通过结构体创建变量的方式有三种:
- struct 结构体名 变量名
- struct 结构体名 变量名 = { 成员1值 , 成员2值…}
- 定义结构体时顺便创建变量
//定义
struct Student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
}s3; //定义结构体时顺便创建变量
//创建
struct Student s1; //可省略struct关键字
s1.name = "Tony";
struct Student s2 = {"Jack", 10, 100};
结构体带构造函数
一种构造函数
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
创建
ListNode l(0); //创建结构体
//struct Student s2 = {"Jack", 10, 100}; //出错
ListNode* l2 = new ListNode(2); //创建指向结构体的指针
多种构造函数
struct Student
{
string name;
int age;
int score;
Student(){}
Student(int a):name("Jack"), age(a), score(100){}
Student(string s, int x, int y){}
};
//等价
struct Student s1 = {0};
struct Student s1(0);
//等价
struct Student s2("Jack", 10, 100);
struct Student s2 = {"Jack", 10, 100};
8.2 结构体数组
作用: 将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }
- 无构造函数
struct Student stuArray[3] =
{
{"Kary", 18, 100},
{"Milk", 20, 99},
{"Juily", 20, 98}
}; //定义
stuArray[2].age = 19; //赋值
- 有构造函数
struct Student
{
string name;
int age;
int score;
Student(int x){}
};
struct Student s[3]; //出错
struct Student s[3]={{0},{0},{0}};
8.3 结构体指针
作用:通过指针访问结构体中的成员
利用操作符 ->可以通过结构体指针访问结构体属性
Student s1 = {"Tom", 18, 100};
//通过指针指向结构体变量
struct student * p = &s1; //struct可省略
//访问结构体变量中的数据
p->name="Tonny"; //等同于 s1.name="Tonny"
8.4 结构体的嵌套
作用: 结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体
struct teacher
{
int id;
string name;
int age;
struct student stu;
};
int main(){
teacher t ={1001, "老王", 20, s3};
t.s3.score = 100;
}
8.5 结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式有两种:
- 值传递
- 地址传递
//值传递
void printStu(struct student s){
s.age = 100; //不改变原结构体的值
cout << s.age << endl;
}
//地址传递
void printStu2(struct student *p){
p->age = 100; //改变原结构体的值
cout << p->age << endl;
}
int main(){
student s1;
printStu(s1);
printStu(&s1);
}
关键字
const
1 修饰变量,说明该变量不可以被改变;
2 修饰指针
- const修饰指针 – 常量指针(const pointer);
- const修饰常量 – 指针常量(pointer to const);
- 即修饰指针,又修饰常量;
常量指针
int a = 10;
const int *p = &a; //常量指针
指针的指向可以修改,但指针指向的值不可以修改
*p = 20; // 修改指针指向的值,不可以
p = &b; // 修改指针的指向,可以
指针常量
int a = 10;
int *const p = &a; // 指针常量
指针的指向不可以改,但指针指向的值可以改
*p = 20; // 修改指针指向的值,可以
p = &b; // 修改指针的指向,不可以
即修饰指针,又修饰常量
int a = 10;
const int *const p = &a; // 指针常量
指针的指向不可以改,但指针指向的值也不可以改
3 修饰引用,指向常量的引用(reference to const),用于修饰形参,防止误操作
int a = 10;
int & ref = 10; //出错,必须饮用一块合法的引用空间
const int & ref = 10; // 正确
void showValue(int & val){
val = 1000; //原a值被修改
cout << val << endl;
}
void showValue(const int & val){
val = 1000; //报错,不能修改
cout << val << endl;
}
int main(){
int a = 100;
showValue(a);
}
4 修饰成员函数
- 常函数
常函数不可以修改成员属性,成员属性声明时加关键字mutable后,在常函数中依然可修改
class Person
{
public:
int m_A;
mutable int m_B;
// this指针的本质:指针常量,指针的指向不可以修改
// 成员函数后面加const,修饰的是this指针,让指针指向的值也不可以被修改
void showPerson() const //常函数
{
m_A = 100; //报错
m_B = 100; //不报错
}
void func(){} //普通函数
}
- 常对象
常对象只能调用常函数
int main(){
const Person p; // 在对象前加const,变为常对象
p.m_A = 100; //报错,不允许修改
p.m_B = 100; //可以修改,特殊值
p.func(); //出错,常对象只能调用常函数
}
// 函数
void function1(const int Var); // 传递过来的参数在函数内不可变
void function2(const char* Var); // 参数指针所指内容为常量
void function3(char* const Var); // 参数指针为常量
void function4(const int& Var); // 引用参数在函数内为常量
// 函数返回值
const int function5(); // 返回一个常数
const int* function6(); // 返回一个指向常量的指针变量,使用:const int *p = function6();
int* const function7(); // 返回一个指向变量的常指针,使用:int* const p = function7();
static
- 修饰普通变量,修改变量的存储区域和生命周期,使变量存储在静态区,在 main 函数运行前就分配了空间,如果有初始值就用初始值初始化它,如果没有初始值系统用默认值初始化它。
- 修饰普通函数,表明函数的作用范围,仅在定义该函数的文件内才能使用。在多人开发项目时,为了防止与他人命名空间里的函数重名,可以将函数定位为 static。
- 修饰成员变量,修饰成员变量使所有的对象只保存一个该变量,而且不需要生成对象就可以访问该成员。
- 修饰成员函数,修饰成员函数使得不需要生成对象就可以访问该函数,但是在 static 函数内不能访问非静态成员。
1. 内存分区模型
C++程序在执行时,将内存大方向划分为4个区域
- 代码区:存放函数体的二进制代码,由操作系统进行管理的
- 全局区:存放全局变量和静态变量以及常量
- 栈区:由编译器自动分配释放, 存放函数的参数值,局部变量等
- 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
内存四区意义:
不同区域存放的数据,赋予不同的生命周期, 给我们更大的灵活编程
1.1 程序运行前
在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域
代码区:
存放 CPU 执行的机器指令
- 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
- 代码区是只读的,使其只读的原因是防止程序意外地修改了它的指令
全局区:
全局变量和静态变量存放在此,全局区还包含了常量区, 字符串常量和其他常量也存放在此
该区域的数据在程序结束后由操作系统释放
//全部变量
int g_a;
int g_b;
const int g_c = 10;
int main(){
//局部变量
int a;
int b;
//静态变量
static int s_a;
static int s_b;
//常量:字符串常量/const修饰的变量
const int c_a = 10;
cout ...
}
全局变量g_a
0x10c6fa060
0x10c6fa064
局部变量a
0x7ffee350796c
0x7ffee3507968
静态变量s_a
0x10c6fa068
0x10c6fa06c
字符串常量"hello"
0x10c6f9f90
const常量
0x10e53ef98
0x7ffee3507964
全局变量/静态变量/字符串变量/const修饰的全局变量 均在全局区
局部变量/const修饰的局部变量 不在全局区
1.2 程序运行后
栈区:
由编译器自动分配释放, 存放函数的参数值,局部变量等
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
局部变量,存放在栈区,栈区的数据在程序执行完后自动释放
int * func(int b) //形参数据也会放在栈区
{
b = 100;
int a = 10; //局部变量,存放在栈区,程序执行完后自动释放
return &a; //返回局部变量的地址
}
int main() {
int *p = func(1);
cout << *p << endl; // 第一次打印正确是编译器做了保留,返回10
cout << *p << endl; // 乱码
return 0;
}
堆区
由程序员分配释放,若程序员不释放,程序结束时由操作系统回收
在C++中主要利用new在堆区开辟内存
int* func()
{
// 不用new, 栈区
// int a = 10;
// return &a;
// 利用new关键字, 将数据开辟到栈区
int* a = new int(10);
return a;
}
int main() {
int *p = func();
cout << *p << endl;
cout << *p << endl;
return 0;
}
1.3 new操作符
C++中利用new操作符在堆区开辟数据
堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符 delete
语法:new 数据类型
利用new创建的数据,会返回该数据对应的类型的指针
int* func()
{
int* a = new int(10);
return a;
}
int main() {
int *p = func();
cout << *p << endl;
cout << *p << endl;
//堆区的数据,由程序员管理开辟,程序员管理释放
//利用delete释放堆区数据
delete p;
cout << *p << endl; //报错,释放的空间不可访问
return 0;
}
堆区开辟数组
int main() {
// 创建数组
int* arr = new int[10];
for (int i = 0; i < 10; i++)
{
arr[i] = i + 100;
}
//释放数组 delete 后加 []
delete[] arr;
return 0;
}
2. 引用
2.1 引用的基本使用
作用:给变量起别名
语法: 数据类型 &别名 = 原名
int main() {
int a = 10;
int &b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
b = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
2.2 引用注意事项
引用必须初始化
引用在初始化后,不可以改变
int a = 10;
int b = 20;
int &c; //错误,引用必须初始化
int &c = a;
c = b; //这是赋值操作,不是更改引用
2.3 引用做函数参数
作用:函数传参时,可以利用引用的技术让形参修饰实参
优点可以简化指针修改实参
//1. 值传递
void swap01(int a, int b) {
int temp = a;
a = b;
b = temp;
}
//2. 地址传递
void swap02(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
//3. 引用传递
void swap03(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 10;
int b = 20;
swap01(a, b);
cout << "a:" << a << " b:" << b << endl;
swap02(&a, &b);
cout << "a:" << a << " b:" << b << endl;
swap03(a, b);
cout << "a:" << a << " b:" << b << endl;
return 0;
}
2.4 引用做函数返回值
作用:引用是可以作为函数的返回值存在的
注意:不要返回局部变量引用
用法:函数调用作为左值
//返回局部变量引用
int& test01() {
int a = 10; //局部变量
return a;
}
//返回静态变量引用
int& test02() {
static int a = 20;
return a;
}
int main() {
//不能返回局部变量的引用
int& ref = test01();
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl; //乱码
int& ref2 = test02();
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
// 函数调用作为左值
test02() = 1000;
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
return 0;
}
2.5 引用的本质
本质:引用的本质在c++内部实现是一个指针常量.
//发现是引用,转换为 int* const ref = &a;
void func(int& ref){
ref = 100; // ref是引用,转换为*ref = 100
}
int main(){
int a = 10;
//自动转换为 int* const ref = &a; 指针常量是指针指向不可改,也说明为什么引用不可更改
int& ref = a;
ref = 20; //内部发现ref是引用,自动帮我们转换为: *ref = 20;
func(a);
}
3. 函数提高
3.1 函数默认参数
在C++中,函数的形参列表中的形参是可以有默认值的。
语法:返回值类型 函数名 (参数= 默认值){}
//1. 如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值
int func(int a, int b = 10, int c = 10) {
return a + b + c;
}
//2. 如果函数声明有默认值,函数实现的时候就不能有默认参数
int func2(int a = 10, int b = 10);
int func2(int a, int b) {
return a + b;
}
int main() {
cout << "ret = " << func(20, 20) << endl;
cout << "ret = " << func(100) << endl;
return 0;
}
3.2 函数占位参数
C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
语法: 返回值类型 函数名 (数据类型){}
//函数占位参数 ,占位参数也可以有默认参数
void func(int a, int) {
cout << "this is func" << endl;
}
int main() {
func(10,10); //占位参数必须填补
return 0;
}
3.3 函数重载
3.3.1 函数重载概述
作用:函数名可以相同,提高复用性
函数重载满足条件:
- 同一个作用域下
- 函数名称相同
- 函数参数类型不同 或者 个数不同 或者 顺序不同
注意: 函数的返回值不可以作为函数重载的条件
//函数重载需要函数都在同一个作用域下
void func()
{
cout << "func 的调用!" << endl;
}
void func(int a)
{
cout << "func (int a) 的调用!" << endl;
}
void func(double a)
{
cout << "func (double a)的调用!" << endl;
}
void func(int a ,double b)
{
cout << "func (int a ,double b) 的调用!" << endl;
}
void func(double a ,int b)
{
cout << "func (double a ,int b)的调用!" << endl;
}
//函数返回值不可以作为函数重载条件
//int func(double a, int b)
//{
// cout << "func (double a ,int b)的调用!" << endl;
//}
int main() {
func();
func(10);
func(3.14);
func(10,3.14);
func(3.14 , 10);
return 0;
}
3.3.2 函数重载注意事项
- 引用作为重载条件
- 函数重载碰到函数默认参数
//函数重载注意事项
//1、引用作为重载条件
void func(int &a)
{
cout << "func (int &a) 调用 " << endl;
}
void func(const int &a)
{
cout << "func (const int &a) 调用 " << endl;
}
int main() {
int a = 10;
func(a); //调用无const
func(10);//调用有const
return 0;
}
//2、函数重载碰到函数默认参数
void func2(int a, int b = 10)
{
cout << "func2(int a, int b = 10) 调用" << endl;
}
void func2(int a)
{
cout << "func2(int a) 调用" << endl;
}
int main(){
func2(10); //碰到默认参数产生歧义,需要避免, 二义性
return 0;
}
本文围绕C++展开,介绍了结构体的定义、数组、指针、嵌套及做函数参数的用法,阐述了const和static关键字的作用,讲解了内存分区模型,包括程序运行前后各区域特点及new操作符的使用,还说明了引用的基本使用、注意事项等,以及函数的默认参数、占位参数和重载等内容。

被折叠的 条评论
为什么被折叠?



