#include #include using namespace std; class Target { string s; public: Target(string s) : s(s) {} virtual ~Target() = default; virtual void request(const string& contents) { cout << s << " send request to server: " << contents << endl; } }; class Adaptee { public: int value; Adaptee(int value) : value(value) {} }; class Adapter : public Target { shared_ptr a; public: Adapter(shared_ptr a) : Target("User " + to_string(a->value)), a(a) {} virtual void request(const string& contents) override { cout << "Adapted request" << endl; Target::request(contents); } }; int main() { shared_ptr t = make_shared("User A"); t->request("Hello"); shared_ptr adaptee = make_shared(42); // Obviously this is invalid. // a->request(); // But it works with adapter. shared_ptr adapter = make_shared(adaptee); adapter->request("Bye"); }