4/4 异常

发布时间 2023-04-04 23:11:03作者: 逆向狗

1.c++类型转换
![[HR9Q)FZVH6_[3I]SXXX}01Q.jpg]]
![[3_B3B_OCF@J`WE]_1L1K63O.jpg]]
1.静态转换
用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换

father* f = NULL;
son* s = NULL;
//向上 将子类转换为父类
father* f1 = static_cast<father>(s);
//向下 将父类转为子类
son
ss = static_cast<son*>(f);
用于基本数据类型之间的转换 如int char
char zzz = 'a';
int aa = static_cast(zzz);
引用转换
father f;
son s;
father& m = f;
son& z = s;
static_cast<father&>(z);
//向上 将子类转换为父类
static_cast<father&>(z);
//向下 将父类转为子类
static_cast<son&>(f);
///引用
动态转换
1.不支持普通类型
2.对向下造造型检查更为严格
dynamic_cast<son
>(f); erro必须多态

多态条件 1.虚函数 2.继承3.覆盖父类函数

多态下支持动态转换

3,.常量转换
1.常量指针被转化成非常量指针 ,并且仍然指向原来的对象
2.常量引用被转化成非常量指针 ,并且仍然指向原来的对象
const int* p = NULL;
int* pp = const_cast<int>(p);
4.重新解释转换
int a = 10;
int
p = reinterpret_cast<int* >(a);

![[8QCSSNMSJDZK3J{O}RM{J@M.jpg]]
1.为什么引入异常
![[%WQ{%1P`]@7YQOF{~UHWZHV 1.jpg]]
if(arr=null)
{
return -1;
}
2.异常的基本语法
void test()
{

throw 10;

}

int main()
{
try
{
test();
}
catch (int)
{
printf("接受到了int 异常");
}

}

3. 异常的运行流程
汇编查看
抛出异常会跳过一些异常代码
![[64B7K`BVVZ%E_R)L[LBO)B5.png]]

![[P@000}[}BX$QYL2Y}}$J639.png]]

4.异常的优势
1.明确错误信息
2.异常不能忽略
3.异常向上抛出

include

include<string.h>

void test2()
{
throw 10;

}
void test()
{

try
{
	test2();
}
catch (int)
{
	throw;
}

}

int main()
{

try
{
	test();
}
catch (int)
{
	printf("3");;
}

}

![[8QCSSNMSJDZK3J{O}RM{J@M.jpg]]
1.为什么引入异常
![[%WQ{%1P`]@7YQOF{~UHWZHV 1.jpg]]
if(arr=null)
{
return -1;
}
2.异常的基本语法
void test()
{

throw 10;

}

int main()
{
try
{
test();
}
catch (int)
{
printf("接受到了int 异常");
}

}

3. 异常的运行流程
汇编查看
抛出异常会跳过一些异常代码
![[64B7K`BVVZ%E_R)L[LBO)B5.png]]

![[P@000}[}BX$QYL2Y}}$J639.png]]

4.异常的优势
1.明确错误信息
2.异常不能忽略
3.异常向上抛出

include

include<string.h>

void test2()
{
throw 10;

}
void test()
{

try
{
	test2();
}
catch (int)
{
	throw;
}

}

int main()
{

try
{
	test();
}
catch (int)
{
	printf("3");;
}

}