class PureVirtual
{
public: virtual PureVirtual& Foo () = 0;
virtual ~PureVirtual () {}
};
class SemiVirtual : public PureVirtual
{
public: PureVirtual& Foo () { printf ("foo worked."); return *this; }
virtual ~SemiVirtual () {}
};
class NonVirtual : public SemiVirtual
{
public: NonVirtual& Bar () { printf ("bar worked."); return *this; }
};
TEST (Virtualism, Tests)
{
PureVirtual& pv = NonVirtual ().Bar().Foo (); <-- Works
pv.Foo (); // <- Crashes
}
pv.Foo crashes because pv instance has been disposed. How can i prevent this situation, and invoke the foo function without using pointers but by reference?