fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Interface
  6. {
  7. public:
  8. virtual void print() = 0;
  9. };
  10.  
  11. class Derived : public Interface
  12. {
  13. public:
  14. virtual void print()
  15. {
  16. std::cout << "Derived" << std::endl;
  17. }
  18. };
  19.  
  20. class PossessDerived
  21. {
  22. public:
  23. Interface * getPossession()
  24. {
  25. // Gib Interface-Zeiger auf polymorphes Objekt zurück
  26. return static_cast<Interface *>(&d);
  27. }
  28.  
  29. private:
  30. Derived d;
  31. };
  32.  
  33. int main(int argc, char * argv[])
  34. {
  35. PossessDerived p;
  36.  
  37. p.getPossession()->print();
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
Derived