Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Passing an object as an argument in custom manipulator

varun_789varun_789 Member Posts: 7
hello friends,

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

#include
#include
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.

Comments

  • ledaslledasl Member Posts: 30
    : hello friends,
    :
    : i want to pass an object as an argument as follows.
    :
    : #include
    : #include
    : using namespace std;
    :
    : class myclass
    : {
    : public :
    : int m,n;
    :
    : //myclass(){m=10;n=12;} // this not error
    [red]
    myclass() : m(10), n(12) {} // but this better
    [/red]
    : };
    :
    : ostream & disp( ostream & output,myclass &obj)
    : {
    : output<<"Hello"<<endl;
    : output<<obj.m;
    : return output;
    : }
    :
    : void main()
    : {
    : myclass myobjct;
    : //cout<<disp<<myobjct;
    [red]
    cout << disp(cout, myobjct);
    [/red]
    : }
    :
    :
    : Above code does not run.
    : How to achieve above feature ?
    :
    : plase help me. its urgent.
    : thank you.
    :

  • varun_789varun_789 Member Posts: 7
    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
    #include
    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 ?
  • DonotaloDonotalo Member Posts: 715
    : 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
    : #include
    : 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 ?
    :
    [purple]
    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

    [b][black]cout<<disp(cout, myobjct)<<endl; [/b][/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():[code]
    ostream & [blue]operator <<[/blue]( ostream & output, const myclass &obj)
    {
    output<<"m="<<obj.m<<endl;
    output<<"n="<<obj.n<<endl;
    return output;
    }
    [/code]
    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.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.