943,633 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 500
  • C++ RSS
Aug 21st, 2009
1

operator overloading

Expand Post »
how to overload >>,<< operators
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adsiq is offline Offline
1 posts
since Aug 2009
Aug 21st, 2009
1

Re: operator overloading

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 21st, 2009
0

Re: operator overloading

usually :

C++ Syntax (Toggle Plain Text)
  1. std::ostream& operator <<(std::ostream& ostrm)
  2. {
  3. ostrm << "Hello its overloaded\n";
  4. return ostrm;
  5. }
  6. std::istream& operator >>(std::istream& istrm)
  7. {
  8. cout<<"Enter a number : ";
  9. istrm >> MyClass::myNumber;
  10.  
  11. return istrm;
  12. }

note overloading the << operator should be a friend, so you can use it like this :

cout << myClassVar;

if not then you would use this syntax :

myClassVar <<cout;

which is confusing.
Last edited by firstPerson; Aug 21st, 2009 at 2:59 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 21st, 2009
0

Re: operator overloading

i too have and still have problems with operator overloading, i found the following website helpful. i hope that it does the same for you.


http://www.learncpp.com/cpp-tutorial...tic-operators/
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaneselling is offline Offline
3 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Convert paper money into coins.
Next Thread in C++ Forum Timeline: Double linked List Iterator InsertAfter Function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC