CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Jan 2009
    Posts
    1

    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;
    }
    Last edited by Andreas Masur; January 7th, 2009 at 07:17 AM. Reason: Added code tags...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured