fork download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. class A {
  6. protected:
  7. virtual int get() {
  8. cout << "in A::get" << endl;
  9. return 0;
  10. }
  11. public:
  12. template<typename Function>
  13. A(const Function & get_ptr) {
  14. get_ptr(); // NOT get() !
  15. }
  16. };
  17.  
  18. class B : public A {
  19. protected:
  20. int get() override {
  21. cout << "in B::get" << endl;
  22. return 0;
  23. }
  24. public:
  25. B() : A([=]{ B::get(); })
  26. // ^^^^^^^^ this is undefined behavior, as `this` is not yet a B*!
  27. {
  28. }
  29. };
  30.  
  31. int main() {
  32. B b;
  33. return 0;
  34. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
in B::get