<< operator overloading

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

<< operator overloading

 
0
  #1
Nov 12th, 2008
Can anybody tell me how to overload << operator ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: << operator overloading

 
0
  #2
Nov 12th, 2008
10 seconds with google finds this

But, I would not put the << endl in the overload - that limits how you can use it in building up outputs from multiple object.
Last edited by vmanes; Nov 12th, 2008 at 3:16 am.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 5
Reputation: khotso is an unknown quantity at this point 
Solved Threads: 0
khotso khotso is offline Offline
Newbie Poster

Re: << operator overloading

 
0
  #3
Jul 16th, 2009
in order to be able to overload an operator<<,it must be declared as the friend of the class.
here is the overloarding syntax:
//declaring as the class friend
friend std::ostream operator<<(std::ostream &output,classname out);
//overloading it outside the class
std::ostream operator<<(std::ostream & output,classname out)
{
//function specification here
//if you want to display the word do as for example
output<<"do";
return output;
}
kss
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: << operator overloading

 
0
  #4
Jul 16th, 2009
in order to be able to overload an operator<<,it must be declared as the friend of the class.
Only if the overloaded operator needs access to the private or protected members of the class. If you only touch the public interface, the operator does not need to be a friend:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. namespace Daniweb
  6. {
  7. class Test
  8. {
  9. int _x;
  10. public:
  11. Test(int x): _x(x) {}
  12. int operator()() const { return _x; }
  13. };
  14.  
  15. ostream& operator<<(ostream& os, const Test t)
  16. {
  17. return os << t();
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. cout << Daniweb::Test(15) << '\n';
  24. }
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC