fork download
  1. #include <iostream>
  2.  
  3. namespace exercise {
  4.  
  5. class A {
  6. public:
  7.  
  8. virtual void a() = 0;
  9. virtual void b() = 0;
  10. virtual void c() = 0;
  11.  
  12. };
  13.  
  14. class B : public A {
  15.  
  16. void a() { std::cout << "a function @ B class"; }
  17. void b() { std::cout << "b function @ B class"; }
  18. void c() { std::cout << "c function @ B class"; }
  19.  
  20. };
  21.  
  22. class C : public A {
  23.  
  24. void a() { std::cout << "a function @ C class"; }
  25. void b() { std::cout << "b function @ C class"; }
  26. void c() { std::cout << "c function @ C class"; }
  27. };
  28.  
  29. using Pstd_mem = void(A::*)();
  30.  
  31. void foo(A* basePointer, Pstd_mem action) {
  32.  
  33. basePointer->*action();
  34.  
  35. }
  36. }
  37.  
  38. int main(void) {
  39.  
  40. using namespace exercise;
  41.  
  42. A* b = new B();
  43. A* c = new C();
  44.  
  45. foo(b, &A::b);
  46.  
  47. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void exercise::foo(exercise::A*, exercise::Pstd_mem)’:
prog.cpp:33:26: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘action (...)’, e.g. ‘(... ->* action) (...)’
     basePointer->*action();
                          ^
prog.cpp: In function ‘int main()’:
prog.cpp:43:4: warning: unused variable ‘c’ [-Wunused-variable]
 A* c = new C();
    ^
stdout
Standard output is empty