Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- /**
- * I AM NOT ALLOWED TO TOUCH THESE, BECAUSE THEY ARE A PART OF A LIBRARY
- */
- class Base
- {
- public:
- virtual void write() { std :: cout << "I am the base class" << std :: endl; };
- };
- class Derived_one: public Base
- {
- public:
- virtual void write() { std :: cout << "Writing like an angel..." << std :: endl; };
- };
- class Derived_two: public Base
- {
- public:
- virtual void write() { std :: cout << "Writing like an angel...again" << std :: endl; };
- };
- /**
- * MY PART OF CODE; ONLY THIS CAN BE CHANGED
- */
- void write_and_do_other_stuffs( Derived_one* parameter_p )
- {
- std :: cout << "I'm being written after something has happened!" << std :: endl;
- parameter_p -> write();
- }
- void write_and_do_other_stuffs( Derived_two* parameter_p )
- {
- std :: cout << "I'm being written after something else has happened!" << std :: endl;
- parameter_p -> write();
- }
- void message_of_instances( const std :: vector<Base*>& instances_p )
- {
- for( auto& instance: instances_p )
- {
- // I need to do this many times but this does not compile:
- write_and_do_other_stuffs( instance );
- // This does compile though:
- // instance -> write();
- }
- }
- // Handling four instances of parameters, all of them can be either Derived_one or Derived_two
- template<typename TH_type_1, typename TH_type_2, typename TH_type_3, typename TH_type_4>
- 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 )
- {
- const std :: vector<Base*> instances = { parameter_1_p, parameter_2_p, parameter_3_p, parameter_4_p };
- /*
- This would work, but I need to do this in way too many situations to be practical:
- write_and_do_other_stuffs( parameter_1_p );
- write_and_do_other_stuffs( parameter_2_p );
- write_and_do_other_stuffs( parameter_3_p );
- write_and_do_other_stuffs( parameter_4_p );
- */
- message_of_instances( instances );
- }
- int main()
- {
- Derived_one* instance_1 = new Derived_one;
- Derived_one* instance_2 = new Derived_one;
- Derived_two* instance_3 = new Derived_two;
- Derived_two* instance_4 = new Derived_two;
- function_call_with_different_types<Derived_one*, Derived_one*, Derived_two*, Derived_two*>( instance_1, instance_2, instance_3, instance_4 );
- delete instance_1;
- delete instance_2;
- delete instance_3;
- delete instance_4;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement