Output stream Operator Overloading for Enum not working in ostream_iterator
Dear Friends,
If ostream_iterator points to a object, it calls corresponding operator overloaded function for that object, but if it points to an enum it is not calling corresponding operator overloaded function.Why?
Please let me know why it is not calling corresponding overloaded function and help in resolving it.
Thanks in advance.
Please find the code below.
Code:
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
enum Result { NO=0,YES=1 };
ostream& operator<<(ostream& os, const Result out) {
switch(out) {
default:
case YES: return os << "Enum Operator Overlaoding -> yes" << endl;
case NO: return os << "Enum Operator Overlaoding -> no" << endl;
}
}
class A
{
};
ostream& operator<<(ostream& os, const A out) {
return os << "Operator Overloading -> A" << endl;
}
int main()
{
ostream_iterator<A> oi_object(cout);
A a;
*oi_object=a; // Calling Operator "<<" Overloaded function of Class A
ostream_iterator<Result> oi_enum(cout);
*oi_enum=NO; // Not Calling Operator "<<" Overloaded function of Enum why? instead prints 0.
return 0;
}
Re: Output stream Operator Overloading for Enum not working in ostream_iterator
I built your code with VS2008, and the output when running is:
Operator Overloading -> A
Enum Operator Overlaoding -> no