fork(1) download
  1. #include <map>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. // Forward declarations needed for defining the global functions.
  6. class typeX;
  7. class typeY;
  8.  
  9. // Global functions with call by pointer (or reference) to avoid copying objects.
  10. void func(const typeX*) { std::cout << "::func(const typeX&)" << std::endl; }
  11. void func(const typeY*) { std::cout << "::func(const typeY&)" << std::endl; }
  12.  
  13. // Abstract base class.
  14. class Base {
  15. public:
  16. virtual void func() = 0;
  17. };
  18.  
  19. void Base::func() {};
  20.  
  21. // Derived class as replacement for your original typeX.
  22. class typeX : public Base {
  23. public:
  24. void func() { std::cout << "typeX::func()" << std::endl; ::func(this); };
  25. int i; // Example for your original 'typeX' content.
  26. };
  27.  
  28. // Derived class as replacement for your original typeY.
  29. class typeY : public Base {
  30. public:
  31. void func() { std::cout << "typeY::func()" << std::endl; ::func(this); };
  32. std::string s; // Example for your original 'typeY' content.
  33. };
  34.  
  35. int main() {
  36. typeX A, B;
  37. typeY Z, Y;
  38.  
  39. std::map<std::string, Base*> map;
  40. map["a"] = &A;
  41. map["z"] = &Z;
  42.  
  43. std::vector<std::string> list { "a", "z" };
  44. for (auto const &list_item : list)
  45. map[list_item]->func();
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
typeX::func()
::func(const typeX&)
typeY::func()
::func(const typeY&)