fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. struct A {
  5. A() { test(); }
  6. void test() { f(); }
  7. virtual void f() = 0;
  8. };
  9.  
  10. void A::f() {
  11. std::cout << "A::f()" << std::endl;
  12. }
  13.  
  14. struct B : public A{
  15. virtual void f() {
  16. A::f(); // <----- call default implementation
  17. std::cout << "B::f()" << std::endl;
  18. }
  19. };
  20.  
  21. extern "C" void __cxa_pure_virtual() {
  22. throw std::runtime_error("Oh shit. I just called a pure virtual function ;(");
  23. }
  24.  
  25. int main() {
  26. try {
  27. A *a = new B();
  28. a->f();
  29. } catch (std::exception &e) {
  30. std::cout << "Exception caught: " << e.what() << std::endl;
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
Exception caught: Oh shit. I just called a pure virtual function ;(