Search code examples
c++constructorderived-class

Prevent Base class constructor being called on invalid Derived class initializer


Consider the following example where the construction of Derived class takes a pointer on its constructor's initializer list. Of course I want to check if this pointer is valid and throw an exception otherwise.

My attempt prevents the program to crash but Base part is still constructed before I can throw the exception.

Is there a way I can prevent Base class constructor being called in this case ?

#include <stdlib.h>
#include <iostream>

class Base
{
public:
    Base(int val) : val_b(val)
    {
        std::cout << "Base::CTOR" << std::endl;
    }
    ~Base() { }

    int val_b;
};

class Derived : public Base
{
public:
    Derived(int *, int);
    ~Derived() { }

    int val_d;

    void print(void)
    {
        std::cout << "Base:\t" << val_b << std::endl;
        std::cout << "Derived:" << val_d << std::endl;
    }

};

Derived::Derived(int *val1, int val2) : Base(val1 ? *val1 : -1), val_d(val2) 
{
    if (!val1)
    {
        throw std::invalid_argument("bad pointer");
    }
}

int main()
{
    int *a = NULL;  
    int b = 43;

    try 
    {
        Derived *d = new Derived(a, b);
        d->print();
    }
    catch (std::exception &e)
    {
        std::cout << "Exception: " << e.what() << std::endl;
    }

    return 0;
}

Solution

  • You might call a function/lambda before calling Base constructor:

    Derived::Derived(int *val1, int val2) :
        Base([&](){
            if (!val1) {
                throw std::invalid_argument("bad pointer");
            }
            return *val1;
        }()),
        val_d(val2) 
    {
    }