Hard
Adapter stored in a std::vector of the target type
What does this program print?
main.cpp
struct Target { virtual std::string request() const { return "default"; } virtual ~Target() = default; };
class LegacyPrinter {
public:
std::string legacyPrint() const { return "legacy output"; }
};
class PrinterAdapter : public Target {
public:
explicit PrinterAdapter(LegacyPrinter p) : legacy_(std::move(p)) {}
std::string request() const override { return legacy_.legacyPrint(); }
private:
LegacyPrinter legacy_;
};
int main() {
std::vector<Target> targets; // container of Target BY VALUE
targets.push_back(PrinterAdapter(LegacyPrinter{}));
std::cout << targets[0].request() << "\n";
}28 of 44 in Design Patterns