fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Base
  5. {
  6. public:
  7. Base(std::string (*funcPointer)()) { execute = funcPointer; }
  8.  
  9. std::string (*execute)();
  10. };
  11.  
  12. std::string StandaloneExecute()
  13. {
  14. return std::string("StandaloneExecute");
  15. }
  16.  
  17. class Derived: public Base
  18. {
  19. public:
  20. Derived() : Base(StandaloneExecute) {}
  21.  
  22. std::string execute() //this hides the parent execute
  23. {
  24. return std::string("Derived::Execute");
  25. }
  26. };
  27.  
  28.  
  29. int main() {
  30. Derived d;
  31. std::cout << d.execute() << std::endl; //this will print Derived::Execute
  32. std::cout << static_cast<Base>(d).execute() << std::endl; //this will print StandaloneExecute
  33. return 0;
  34. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Derived::Execute
StandaloneExecute