小白求助 关于异常对象的生命周期
#include "iostream"using std::endl;
using std::cout;
using std::cin;
class BadSrcType{};
class BadDestType{};
class BadProcessType
{
public:
BadProcessType()
{
cout << "BadProcessType构造执行了\n";
}
BadProcessType(const BadProcessType& bp)
{
cout << "BadProcessType copy 构造执行了\n";
}
~BadProcessType()
{
cout << "~BadProcessType析构执行了\n";
}
};
void my_strcpy(char* from, char* to)
{
if (from == nullptr)
{
throw BadSrcType();
}
if (to == nullptr)
{
throw BadDestType();
}
if (*from == 'a')
{
throw BadProcessType();
}
if (*from == 'b')
{
throw& (BadProcessType());
}
}
void main()
{
char buf1[] = "bacdefg";
char buf2;
try
{
my_strcpy(buf1, buf2);
}
catch (BadProcessType e)//问题1:当这个catch捕捉到异常的时候 为啥catch运行完了 异常对象和e对象都析构?
{
cout << "BadProcessTypece类型错误\n";
}
catch (BadProcessType * e)//问题2:为啥在这个catch捕捉到异常之前 异常对象已经析构了?
{
cout << "BadProcessTypece*类型错误\n";
}
system("pause");
} 顶
页:
[1]