Advertisement
Hunyadix

Polymorphism problem, StackOverflow

Dec 5th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. /**
  4.  * I AM NOT ALLOWED TO TOUCH THESE, BECAUSE THEY ARE A PART OF A LIBRARY
  5.  */
  6.  
  7. class Base
  8. {
  9.     public:
  10.         virtual void write() { std :: cout << "I am the base class" << std :: endl; };
  11. };
  12.  
  13. class Derived_one: public Base
  14. {
  15.     public:
  16.         virtual void write() { std :: cout << "Writing like an angel..." << std :: endl; };
  17. };
  18.  
  19. class Derived_two: public Base
  20. {
  21.     public:
  22.         virtual void write() { std :: cout << "Writing like an angel...again" << std :: endl; };
  23. };
  24.  
  25. /**
  26.  * MY PART OF CODE; ONLY THIS CAN BE CHANGED
  27.  */
  28.  
  29. void write_and_do_other_stuffs( Derived_one* parameter_p )
  30. {
  31.     std :: cout << "I'm being written after something has happened!" << std :: endl;
  32.     parameter_p -> write();
  33. }
  34.  
  35. void write_and_do_other_stuffs( Derived_two* parameter_p )
  36. {
  37.     std :: cout << "I'm being written after something else has happened!" << std :: endl;
  38.     parameter_p -> write();
  39. }
  40.  
  41. void message_of_instances( const std :: vector<Base*>& instances_p )
  42. {
  43.     for( auto& instance: instances_p )
  44.     {
  45.         // I need to do this many times but this does not compile:
  46.         write_and_do_other_stuffs( instance );
  47.         // This does compile though:
  48.         // instance -> write();
  49.     }
  50. }
  51.  
  52. // Handling four instances of parameters, all of them can be either Derived_one or Derived_two
  53. template<typename TH_type_1, typename TH_type_2, typename TH_type_3, typename TH_type_4>
  54. void function_call_with_different_types( TH_type_1 parameter_1_p, TH_type_2 parameter_2_p, TH_type_3 parameter_3_p, TH_type_4 parameter_4_p )
  55. {
  56.     const std :: vector<Base*> instances = { parameter_1_p, parameter_2_p, parameter_3_p, parameter_4_p };
  57.     /*
  58.         This would work, but I need to do this in way too many situations to be practical:
  59.         write_and_do_other_stuffs( parameter_1_p );
  60.         write_and_do_other_stuffs( parameter_2_p );
  61.         write_and_do_other_stuffs( parameter_3_p );
  62.         write_and_do_other_stuffs( parameter_4_p );
  63.      */
  64.     message_of_instances( instances );
  65. }
  66.  
  67. int main()
  68. {
  69.     Derived_one* instance_1 = new Derived_one;
  70.     Derived_one* instance_2 = new Derived_one;
  71.     Derived_two* instance_3 = new Derived_two;
  72.     Derived_two* instance_4 = new Derived_two;
  73.     function_call_with_different_types<Derived_one*, Derived_one*, Derived_two*, Derived_two*>( instance_1, instance_2, instance_3, instance_4 );
  74.     delete instance_1;
  75.     delete instance_2;
  76.     delete instance_3;
  77.     delete instance_4;
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement