I'm deriving a class which is available from a C++ library, and the constructor of my subclass will only work properly when I execute some code before the base class constructor gets called. (Yes, I know, bad design, but I cannot influence how the library which I'm using works.)
If the base class constructor takes arguments, doing this is actually quite simple:
struct A { A(bool a) { printf("A::A()\n"); } }; bool inject(bool a) { printf("inject()\n"); return a; } struct B : public A { B(bool a) : A(inject(a)) { printf("B::B()\n"); } };
Now, when I construct an instance of B, inject() gets called before A::A(). But is there a way to do this when the base class ctor has no arguments?