catching polymorphic type 'class std::exception' by value [-Wcatch-value=]

Posted sageloris

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了catching polymorphic type 'class std::exception' by value [-Wcatch-value=]相关的知识,希望对你有一定的参考价值。

class Person
public:
    Person()
        mAge = 0;
    
    void setAge(int age)
        if(age < 0 || age > 100)
            throw out_of_range("年龄应该在0-100之间!");
        
        this->mAge = age;
    
public:
    int mAge;
;

void test01()
    Person p;
    try
        p.setAge(1024);
    catch(out_of_range& e)//此处报错
        cout << e.what() << endl;
    

gcc编译报错catching polymorphic type ‘class std::exception‘ by value [-Wcatch-value=];

以上是关于catching polymorphic type 'class std::exception' by value [-Wcatch-value=]的主要内容,如果未能解决你的问题,请参考以下文章

Java:简述try-catch-finally异常捕获

Java:简述try-catch-finally异常捕获

java继承与多态

Rails 如何为多态关联填充“model_type”字段?

如何在 Rails 4 的模型中访问 polymorphic_path?

如何访问`std::variant`的任何孩子的`polymorphic`基类?