| | |
<< operator overloading
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
10 seconds with google finds this
But, I would not put the
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jul 2009
Posts: 5
Reputation:
Solved Threads: 0
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;
}
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
•
•
•
•
in order to be able to overload an operator<<,it must be declared as the friend of the class.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; namespace Daniweb { class Test { int _x; public: Test(int x): _x(x) {} int operator()() const { return _x; } }; ostream& operator<<(ostream& os, const Test t) { return os << t(); } } int main() { cout << Daniweb::Test(15) << '\n'; }
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- a small prob with operator overloading (C++)
- Operator Overloading HELP! (C++)
- about operator overloading (C++)
- operator overloading (Java)
- Operator Overloading Question (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- program for finding factorial of a number using *operator overloading (C++)
Other Threads in the C++ Forum
- Previous Thread: Including LibTorrent .dll in Visual Studio 2008 project
- Next Thread: Using tchar to convert lowercase to uppercase
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






