| | |
<< 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 |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






