Skip to content

Instantly share code, notes, and snippets.

@dgreceanu
Created August 12, 2013 09:35
Show Gist options
  • Save dgreceanu/6209512 to your computer and use it in GitHub Desktop.
Save dgreceanu/6209512 to your computer and use it in GitHub Desktop.
This is a way to get polymorphic behaviour for operator <<.
#include <iostream>
struct A
{
virtual std::ostream& put(std::ostream& o ) const { return o << 'A'; };
};
struct B : A
{
virtual std::ostream& put(std::ostream& o ) const { return o << 'B'; };
};
std::ostream& operator <<(std::ostream& o, const A& a)
{
return a.put(o);
}
int main () {
B b;
std::cout << b << std::endl ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment