C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Passing an object as an argument in custom manipulator Posted by varun_789 on 6 Apr 2006 at 9:17 PM
hello friends,

i want to pass an object as an argument as follows.

#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<<"Hello"<<endl;
output<<obj.m;
return output;
}

void main()
{
myclass myobjct;
cout<<disp<<myobjct;
}


Above code does not run.
How to achieve above feature ?

plase help me. its urgent.
thank you.
Report
Re: Passing an object as an argument in custom manipulator Posted by ledasl on 6 Apr 2006 at 10:28 PM
: hello friends,
:
: i want to pass an object as an argument as follows.
:
: #include <iomanip>
: #include <iostream>
: using namespace std;
:
: class myclass
: {
: public :
: int m,n;
:
: //myclass(){m=10;n=12;} // this not error

myclass() : m(10), n(12) {} // but this better

: };
:
: ostream & disp( ostream & output,myclass &obj)
: {
: output<<"Hello"<<endl;
: output<<obj.m;
: return output;
: }
:
: void main()
: {
: myclass myobjct;
: //cout<<disp<<myobjct;

cout << disp(cout, myobjct);

: }
:
:
: Above code does not run.
: How to achieve above feature ?
:
: plase help me. its urgent.
: thank you.
:

Report
Re: Passing an object as an argument in custom manipulator Posted by varun_789 on 19 Apr 2006 at 9:57 PM
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 ?
Report
Re: Passing an object as an argument in custom manipulator Posted by Donotalo on 19 Apr 2006 at 11:06 PM
: 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()




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.