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