#include <iostream>
using namespace std;
class Interface
{
public:
virtual void print() = 0;
};
class Derived : public Interface
{
public:
virtual void print()
{
std::cout << "Derived" << std::endl;
}
};
class PossessDerived
{
public:
Interface * getPossession()
{
// Gib Interface-Zeiger auf polymorphes Objekt zurück
return static_cast<Interface *>(&d);
}
private:
Derived d;
};
int main(int argc, char * argv[])
{
PossessDerived p;
p.getPossession()->print();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKY2xhc3MgSW50ZXJmYWNlCnsKcHVibGljOgogICAgICAgIHZpcnR1YWwgdm9pZCBwcmludCgpID0gMDsKfTsKIApjbGFzcyBEZXJpdmVkIDogcHVibGljIEludGVyZmFjZQp7CnB1YmxpYzoKICAgICAgICB2aXJ0dWFsIHZvaWQgcHJpbnQoKQogICAgICAgIHsKICAgICAgICAgICAgICAgIHN0ZDo6Y291dCA8PCAiRGVyaXZlZCIgPDwgc3RkOjplbmRsOwogICAgICAgIH0KfTsKIApjbGFzcyBQb3NzZXNzRGVyaXZlZAp7CnB1YmxpYzoKICAgICAgICBJbnRlcmZhY2UgKiBnZXRQb3NzZXNzaW9uKCkKICAgICAgICB7CgkJLy8gR2liIEludGVyZmFjZS1aZWlnZXIgYXVmIHBvbHltb3JwaGVzIE9iamVrdCB6dXLDvGNrCiAgICAgICAgICAgICAgICByZXR1cm4gc3RhdGljX2Nhc3Q8SW50ZXJmYWNlICo+KCZkKTsKICAgICAgICB9CiAKcHJpdmF0ZToKICAgICAgICBEZXJpdmVkIGQ7Cn07CiAKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgKiBhcmd2W10pCnsKICAgICAgICBQb3NzZXNzRGVyaXZlZCBwOwogCiAgICAgICAgcC5nZXRQb3NzZXNzaW9uKCktPnByaW50KCk7CiAKICAgICAgICByZXR1cm4gMDsKfQog