Skip to content

Instantly share code, notes, and snippets.

@mralexgray
Created April 8, 2016 19:17
Show Gist options
  • Save mralexgray/6f436a0ff7599c6e5d46bd136c41ccd1 to your computer and use it in GitHub Desktop.
Save mralexgray/6f436a0ff7599c6e5d46bd136c41ccd1 to your computer and use it in GitHub Desktop.
#include <inttypes.h>
#include <stdio.h> // for size_t
class Outputter;
class Printable { public:
virtual void printTo(Outputter &stream) const = 0;
};
class Outputter { public:
Outputter() {}
void printout(const char* c) { printf("%s", c); }
void printout(const Printable& p) { p.printTo(*this); }
};
Outputter log;
class Printer : public Printable { public:
Printer(){}
virtual void printTo(Outputter& pt) const {
return pt.printout("I am a printer");
}
void print() { /* log.printout(*this); */ }
};
Printer pp;
using namespace std;
int main(int argc, char *argv[]) {
log.printout(pp);
pp.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment