Try throw catch 的一个问题

关于http://groups.google.com/group/comp.lang.c++/browse_thread/thread/649c39f943a65a36/e8d328a4a547ff36?lnk=st&q=catch+(const+Mammal+%26m)+&rnum=1#e8d328a4a547ff36

 

这篇讨论有效的揭露了try, throw , catch 的本质的工作方法。

 

源代码如下: 

 #include <iostream>
using namespace std;


class Mammal
{
public:
 Mammal()
 {
  cout << "Constructing Mammal @ " << this << endl;
 }
 Mammal(const Mammal& source)
 {
  cout << "Copy Constructing Mammal @ " << this << " from " <<
   &source << endl;
 }
 ~Mammal()
 {
  cout << "Destructing Mammal @ " << this << endl;
 }
 virtual const char* MyType() const
 {
  return "Mammal";
 }


};


class Cat : public Mammal
{
public:
 Cat()
 {
  cout << "Constructing Cat @ " << this << endl;
 }
 Cat(const Cat& source)
 {
  cout << "Copy Constructing Cat @ " << this << " from " <<
   &source << endl;
 }
 ~Cat()
 {
  cout << "Destructing Cat @ " << this << endl;
 }

 virtual const char* MyType() const
 {
  return "Cat";
 }

 

};


int main(int argc, char *argv[])
{
 try
 {
  Cat fluffy;
  Mammal& fluffyRef = fluffy;
  throw fluffyRef;
 }
 catch (const Mammal &m)
 {
  cout << "Caught a " << m.MyType() << endl;
  return 0;
 }
 
 cout << "Nothing Caught" << endl;
 return 0;


}

 

输出:

 

Constructing Mammal @ 0012FF50
Constructing Cat @ 0012FF50
Copy Constructing Mammal @ 0012FE60 from 0012FF50
Destructing Cat @ 0012FF50
Destructing Mammal @ 0012FF50
Caught a Mammal
Destructing Mammal @ 0012FE60

 

对这个例子我的感悟:

  1. throw 的用法, 它将抛出某个具体的object(异常类),这个类将被catch所捕捉。由于fluffRef是一个Mammal的reference,但是它的instance 其实是一个cat。但是在throw抛出的时候,throw其实跟不管instance 的type,它只是根据抛出对象申明的type 拷贝构造出一个新的临时的对象以作为异常对象。所以,Throw 抛出的时候,不看对象instance的type,而是看这个当前申明的类型
  2.  如果我们将代码改成

 try
 {
  Cat fluffy; 
  Cat& fluffyRef = fluffy;
  throw fluffyRef;
 }
这个时候,当然throw 的是cat。 注意,这个地方throw调用 Mammal和默认构造函数和Cat的拷贝构造函数,不是我所想象的是基类也是调用拷贝构造函数。

 


   

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值