I am trying to call an overriden function from the parent, and found out that it just crashes.
This is quite hard to describe, so here the minimal reproducible code:
#include <iostream>
class A
{
public:
A()
{
init1();
}
void init1()
{
printf("1");
init2();
printf("2");
}
virtual void init2() = 0;
};
class B : public A
{
public:
B()
: A()
{}
void init2() override
{
printf("hello");
}
};
int main()
{
B b;
return 0;
}
On MSVC 2019 it crashes, on http://cpp.sh/ it outputs "1" and then main()
returns 0, but we never see "hello"
or "2"
.
Why does it crash? What happens from a low level point of view? Is
A
trying to call its owninit2()
?Is there a way of doing it, so I don't have to, in every derived class, add
init2()
in its constructor?