异常的多态

lambdaios / 2024-01-15 / 原文


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>


//异常的基类

class father {
public:
    virtual void printm() {

    }
};
//1 有继承
class sonnullptr :public father {
public:
    virtual void printm() {//2. 重写父类的虚函数
        std::cout << "nullptr\n";
    }

};


class sonout :public father {
public:
    virtual void printm() {
        std::cout << "out_of_data\n";
    }
};

void func() {
    throw std::move(sonout());
    throw std::move(sonnullptr());
}

void test() {
    try {
        func();
    }
    catch (father&f) {
        f.printm();
    }
    catch (...) {
        
    }
}

int main(){

    test();

    return 0;
}