
#include <iostream>
using namespace std;
class Ex
{
public:
Ex()
{
cout<<"Ex::Ex()"<<endl;
}
Ex(const Ex&)
{
cout<<"Ex::Ex(const Ex&)"<<endl;
}
Ex& operator=(const Ex& e)
{
cout<<"Ex::operator="<<endl;
return *this;
}
~Ex()
{
cout<<"Ex::~Ex"<<endl;
}
virtual void pisz()
{
cout<<"Ex::pisz()"<<endl;
}
};
class Ex2:public Ex
{
public:
Ex2()
{
cout<<"Ex2::Ex2()"<<endl;
}
Ex2(const Ex2&)
{
cout<<"Ex2::Ex2(const Ex2&)"<<endl;
}
Ex2& operator=(const Ex2& e)
{
cout<<"Ex2::operator="<<endl;
return *this;
}
~Ex2()
{
cout<<"Ex2::~Ex2"<<endl;
}
void pisz()
{
cout<<"Ex2::pisz()"<<endl;
}
};
class A
{
int a;
public:
A(int r):a(r)
{
cout<<"A::A(int)"<<endl;
}
A(const A& r):a(r.a)
{
cout<<"A::A(const A&)"<<endl;
}
A& operator=(const A& r)
{
a=r.a;
cout<<"A::operator="<<endl;
return *this;
}
~A()
{
cout<<"A::~A"<<endl;
}
int get()const
{
return a;
}
};
void fun2(A& a)
{
cout<<"fun2"<<endl;
if(a.get()<0)
throw Ex();
if(a.get()==0)
throw Ex2();
}
void fun(A a)
{
cout<<"fun"<<endl;
try
{
fun2(a);
}
catch(Ex e)
{
e.pisz();
throw e;
}
}
int main()
{
A a(0);
try
{
fun(a);
}
catch(Ex e)
{
e.pisz();
}
return 0;
}
输出结果:using namespace std;
class Ex
{
public:
Ex()
{
cout<<"Ex::Ex()"<<endl;
}
Ex(const Ex&)
{
cout<<"Ex::Ex(const Ex&)"<<endl;
}
Ex& operator=(const Ex& e)
{
cout<<"Ex::operator="<<endl;
return *this;
}
~Ex()
{
cout<<"Ex::~Ex"<<endl;
}
virtual void pisz()
{
cout<<"Ex::pisz()"<<endl;
}
};
class Ex2:public Ex
{
public:
Ex2()
{
cout<<"Ex2::Ex2()"<<endl;
}
Ex2(const Ex2&)
{
cout<<"Ex2::Ex2(const Ex2&)"<<endl;
}
Ex2& operator=(const Ex2& e)
{
cout<<"Ex2::operator="<<endl;
return *this;
}
~Ex2()
{
cout<<"Ex2::~Ex2"<<endl;
}
void pisz()
{
cout<<"Ex2::pisz()"<<endl;
}
};
class A
{
int a;
public:
A(int r):a(r)
{
cout<<"A::A(int)"<<endl;
}
A(const A& r):a(r.a)
{
cout<<"A::A(const A&)"<<endl;
}
A& operator=(const A& r)
{
a=r.a;
cout<<"A::operator="<<endl;
return *this;
}
~A()
{
cout<<"A::~A"<<endl;
}
int get()const
{
return a;
}
};
void fun2(A& a)
{
cout<<"fun2"<<endl;
if(a.get()<0)
throw Ex();
if(a.get()==0)
throw Ex2();
}
void fun(A a)
{
cout<<"fun"<<endl;
try
{
fun2(a);
}
catch(Ex e)
{
e.pisz();
throw e;
}
}
int main()
{
A a(0);
try
{
fun(a);
}
catch(Ex e)
{
e.pisz();
}
return 0;
}
只有本站会员才能查看附件,请 登录
看不懂为什么会这么输出,哪位高手能帮我解释一下?谢谢啦