Medium
A second class added to a CRTP printing mixin
What happens when this program is built and run?
main.cpp
#include <iostream>
#include <string>
template <typename Derived>
struct Printable {
void print() const {
const auto& self = static_cast<const Derived&>(*this);
std::cout << self.label() << "\n";
}
};
struct Invoice : Printable<Invoice> {
std::string id = "INV-1001";
std::string label() const { return "invoice " + id; }
};
struct Receipt : Printable<Invoice> {
int total = 42;
std::string label() const { return "receipt $" + std::to_string(total); }
};
int main() {
Receipt r;
r.print();
}41 of 44 in Design Patterns