4/4 异常

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;
}

}
[[A]R@KH]97IA@3ISUJ0}U[J 1.jpg1.异常严格的类型匹配![[978Y(I6{{I_VH4VEKNGY0OQ.jpg]] 未知异常的匹配 2.异常的接口声明 ![[_1TG3HE5MT~G4X50CTD{N]H 1.jpg]] ]]![[A]R@KH]97IA@3ISUJ0}U[J 2.jpg]]

3.异常的生命周期

三个对象 
1.创建的对象 栈解旋释放 mm zz;///1.创建的对象 栈解旋释放
2.第一个对象的拷贝,用来抛出 throw zz;//2.第一个对象的拷贝,用来抛出
3.2对象的拷贝 当参数 catch (mm Z)
2 3 catch结束被释放

2 3 catch结束被释放
2个对象
1.直接抛出对象-匿名对象
2.参数
 1 2 catch结束被释放
一个对象
1.抛出
参数用引用
 catch结束被释放

注意
对象指针介入--编译器不允许对站中匿名对象取地址

允许对堆区匿名对象取地址操作
throw new mm();//2.第一个对象的拷贝,用来抛出--catch (mm *z)

异常多态 父类引用指向子类对象
用继承
重写父类虚函数
多态异常

include

include<string.h>

class fat
{
public:

virtual void add()=0;

};
class kzz :public fat
{
public:
virtual void add()
{
printf("加法溢出");
}

};
class null :public fat
{
public:
virtual void add()
{
printf("空指针");
}

};
void test2()
{
throw kzz();
}
void test()
{
try
{
test2();
}
catch (fat&f)
{
f.add();
}

}

异常类
1.c++系统提供的标准库

include

include<string.h>

using namespace std;

include

class fat
{
public:
fat(int age)
{
if (age > 100)
{
throw out_of_range("年龄错误");
}
}

};

void test2()
{

}
void test()
{

try
{
	fat(111);
}
catch (out_of_range &z)
{
	cout<<z.what()<<endl;
}

}
int main()
{
test();

}

自写异常

include

include<string.h>

using namespace std;

include

class ycpc :public exception
{
public:
ycpc(const char* ee)
{
this->m_info = string(ee);
}
ycpc(const string ee)
{
this->m_info = ee;
}

  char const* what() const
{
	  return this->m_info.c_str();
}

public:
string m_info;

};
class fat
{
public:
fat(int age)
{
if (age > 100)
{
throw ycpc("年龄错误");
}
}

};

void test2()
{

}
void test()
{

try
{
	fat(111);
}
catch (ycpc&z)
{
	cout<<z.what()<<endl;
}

}
int main()
{
test();

}

posted @   逆向狗  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示