Created
August 12, 2013 09:35
-
-
Save dgreceanu/6209512 to your computer and use it in GitHub Desktop.
This is a way to get polymorphic behaviour for operator <<.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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