Re: Rationale for base class pure virtual function call from ctor/dtor being undefined behaviour.
"Gareth Owen" <gwowen@gmail.com> wrote in message
news:87fwfdikio.fsf@gmail.com...
"MikeWhy" <boat042-nospam@yahoo.com> writes:
Arguably, then, the PVF with implementation is not really useful, but
simply serves as an escape for defining a benign default action during
construction.
With the irony being, as Leigh initially pointed out, this is the one
thing you're not allowed to do...
I'm a bit vexed this moment. At one point, some earlier version of the
following did manage to print "AbstractBase::PureVirtual()." Toggling the
#if now produces no difference. PVF termination is hit, whether the PVF has
an implementation or not.
#include <iostream>
//======================================================
//======================================================
class AbstractBase
{
public:
AbstractBase() { foo(); }
virtual ~AbstractBase() {}
void foo() { PureVirtual(); }
private:
virtual void PureVirtual() = 0;
};
//======================================================
// not so pure
//##########
#if 1
//##########
void AbstractBase::PureVirtual()
{ std::cout << "AbstractBase::PureVirtual().\n";
}
//##########
#endif
//##########
//======================================================
//======================================================
class Derived : public AbstractBase
{
public:
Derived() { }
private:
void PureVirtual()
{ std::cout << "Derived::PureVirtual().\n";
}
};
//======================================================
//======================================================
//======================================================
int main(int argc, char **argv)
{
Derived a;
a.foo();
std::cout << "Hello, world!" << std::endl;
return 0;
}