fork download
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. class T1 {
  6. public:
  7. virtual void f() {
  8. cout<<"I'm f() from T1"<<endl;
  9. }
  10. };
  11. class T2 {
  12. public:
  13. virtual void f() {
  14. cout<<"I'm f() from T2"<<endl;
  15. }
  16. virtual void g() {
  17. cout<<"I'm g() from T2"<<endl;
  18. }
  19. };
  20. class T3 : public T1, public T2 {
  21. public:
  22. void f() {
  23. T2::f();
  24. T1::f();
  25. }
  26. };
  27.  
  28. int main() {
  29. // your code goes here
  30. list<T2*> l{ new T2(), new T3() };
  31. for (auto &x : l) {
  32. x->f();
  33. x->g();
  34. }
  35. cout <<"--"<<endl;
  36. T1* last_one = dynamic_cast<T1*>(l.back()); // last is also T1
  37. last_one->f();
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
I'm f() from T2
I'm g() from T2
I'm f() from T2
I'm f() from T1
I'm g() from T2
--
I'm f() from T2
I'm f() from T1