前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++使用引用捕获异常

C++使用引用捕获异常

作者头像
恋喵大鲤鱼
发布2018-08-13 16:42:56
5010
发布2018-08-13 16:42:56
举报
文章被收录于专栏:C/C++基础C/C++基础

catch子句捕获异常时既可以按值传递,也可以按照引用传递,甚至按照指针传递,但推荐使用引用捕获异常。考察如下程序:

代码语言:javascript
复制
#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {
        cout << "Base's constructor" << endl;
    }

    Base(const Base& rb)
    {
        cout << "Base's copy constructor" << endl;
    }

    virtual void print()
    {
        cout << "Base" << endl;
    }
};

class Derived :public Base
{
public:
    Derived()
    {
        cout << "Derived's constructor" << endl;
    }

    Derived(const Derived& rd):Base(rd)
    {
        cout << "Derived's copy constructor" << endl;
    }

    virtual void print()
    {
        cout << "Derived" << endl;
    }
};

void throwFunc()
{
    Derived d;
    throw d;
}

int main()
{
    try
    {
        throwFunc();
    }
    catch (Base b)
    {
        cout << "Base catched" << endl;
        b.print();
    }
    catch (Derived d)
    {
        cout << "Derived catched" << endl;
        d.print();
    }
    cout << "---------------" << endl;
    try
    {
        throwFunc();
    }
    catch (Base& b)
    {
        cout << "Base catched" << endl;
        b.print();
    }
    catch (Derived& d)
    {
        cout << "Derived catched" << endl;
        d.print();
    }
}

程序运行结果:

代码语言:javascript
复制
Base's constructor
Derived's constructor
Base's copy constructor
Derived's copy constructor
Base's copy constructor
Base catched
Base
---------------
Base's constructor
Derived's constructor
Base's copy constructor
Derived's copy constructor
Base catched
Derived

阅读以上程序,注意以下几点: (1)程序中在函数throwFunc()中构造对象d,先后分别调用基类Base和派生类Derived的构造函数完成对象d的初始化,分别输出Base’s constructor与Derived’s constructor; (2)C++标准要求被作为异常抛出的对象必须被拷贝复制,导致异常对象d在离开作用域时,触发一次临时对象的拷贝构造,程序输出从结果来看,先后调用了基类Base的拷贝构造函数和派生类Derived的拷贝构造函数,分别输出Base’s copy constructor与Derived’s copy constructor; (3)按引用捕获异常比按值捕获异常更加高效。分隔线以上按值捕获异常,导致对象d在传递时再次被拷贝一次,输出Base’s copy constructor,降低了系统效率,使用引用捕获异常可以避免额外的拷贝操作; (4)使用引用捕获异常,可以通过基类对象实现虚函数的虚调用,在运行时提现多态性。

基于效率和多态性的考虑,建议使用引用来捕获异常。


参考文献

[1]陈刚.C++高级进阶教程[M].武汉:武汉大学出版社,2008.10.5用传引用的方式捕获异常

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年08月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考文献
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档