: hi,
:
: i tried to pass the object the way you specified but
: at the end of the output, it gives some garbage value.
:
: here is the code that i executed.
:
: #include <iomanip>
: #include <iostream>
: using namespace std;
:
: class myclass
: {
: public :
: int m,n;
:
: myclass(){m=10;n=12;}
: };
:
: ostream & disp( ostream & output,myclass &obj)
: {
: output<<"m="<<obj.m<<endl;
: output<<"n="<<obj.n<<endl;
: return output;
: }
:
: void main()
: {
: myclass myobjct;
: //cout<<disp<<myobjct;
: cout<<disp(cout, myobjct)<<endl;
: }
:
:
: here is the output :
: m=10
: n=12
: 004777E4 // this is the garbage value
: Press any key to continue
:
: What is the problem ?
:
the function disp() is showing the values of obj.m and obj.n. this function also returned an ostream type object by reference. ur line of code
cout<<disp(cout, myobjct)<<endl; [/black]
is showing the ostream object cout (since it is returning by ur disp() function). this is not the garbage value. also not a meaningful one in this case.
probably u were wanting an operator function to display the object directly via cout <<. then u should have the following function instead of disp():
ostream & operator <<( ostream & output, const myclass &obj)
{
output<<"m="<<obj.m<<endl;
output<<"n="<<obj.n<<endl;
return output;
}
look i have made the obj constant inside the operator function. it is always a good idea to pass a constant reference in a function when the object should not change its value inside that function.
~Donotalo()