dimanche 1 octobre 2017

How to end C++ code directly from a constructor?

I would like my C++ code to stop running with proper object cleanup if a certain condition is met; in a constructor of a class.

class A {
public:
    int somevar;
    void fun() {
        // something
    }
};

class B {
public:
    B() {
        int possibility;
        // some work
        if (possibility == 1) {
            // I want to end the program here
            kill code;
        }
    }
};

int main() {
    A a;
    B b;
    return 0;
}    

How can I terminate my code at that point doing proper cleanup. It's known that, std::exit does not perform any sort of stack unwinding, and no alive object on the stack will call its respective destructor to perform cleanup. So std::exit is not a good idea.

Aucun commentaire:

Enregistrer un commentaire