Member operators operator>>() and operator<<()

Discussion in 'C++' started by Alex Vinokur, Mar 20, 2005.

  1. Alex Vinokur

    Alex Vinokur Guest

    Member operators operator>>() and operator<<() in a program below
    work fine, but look strange.

    Is it possible to define member operators operator>>() and operator<<()
    that work fine and look fine?

    // --------- foo.cpp ---------
    #include <iostream>
    using namespace std;

    class Foo
    {
    private:
    int data_;

    public:
    istream& operator>>(istream& is_o);
    ostream& operator<<(ostream& os_o);

    };


    // -------------
    istream& Foo::eek:perator>> (istream& is_o)
    {
    return is_o >> data_;
    }


    // -------------

    ostream& Foo::eek:perator<<(ostream& os_o)
    {
    return os_o << data_ << endl;
    }

    // -------------
    int main()
    {
    Foo foo;

    foo.operator>>(cin);
    foo.operator<<(cout);

    foo >> cin; // Works fine, but looks weird
    foo << cout; // -------- the same ---------

    // cin >> foo; // Of course invalid in for class Foo
    // cout << foo; // ------------ the same ------------

    return 0;
    }
    // ---------------------------

    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Mar 20, 2005
    #1
    1. Advertising

  2. Alex Vinokur

    John Carson Guest

    "Alex Vinokur" <> wrote in message
    news:
    > Member operators operator>>() and operator<<() in a program below
    > work fine, but look strange.
    >


    Simple (and usual) solution. Don't use member operators. Use friends of the
    class instead.


    --
    John Carson
    John Carson, Mar 20, 2005
    #2
    1. Advertising

  3. Alex Vinokur

    Rayer Guest


    > Member operators operator>>() and operator<<() in a program below
    > work fine, but look strange.
    >
    > Is it possible to define member operators operator>>() and operator<<()
    > that work fine and look fine?
    >
    > // --------- foo.cpp ---------
    > #include <iostream>
    > using namespace std;
    >
    > class Foo
    > {
    > private:
    > int data_;
    >
    > public:
    > istream& operator>>(istream& is_o);
    > ostream& operator<<(ostream& os_o);
    >
    > };
    >
    >
    > // -------------
    > istream& Foo::eek:perator>> (istream& is_o)
    > {
    > return is_o >> data_;
    > }
    >
    >
    > // -------------
    >
    > ostream& Foo::eek:perator<<(ostream& os_o)
    > {
    > return os_o << data_ << endl;
    > }

    I cant sure weather you look be fine, but how about this?
    istream &Foo::eek:perator>>(istream &is_o)
    {
    iso_o >> data_;
    return iso_o;
    }

    ostream &Foo::eek:perator<<(ostream &os_o)
    {
    os_o << data_ << endl;
    return os_o;
    }

    I think it will express better, and how do you think 'bout it?
    >
    > // -------------
    > int main()
    > {
    > Foo foo;
    >
    > foo.operator>>(cin);
    > foo.operator<<(cout);
    >
    > foo >> cin; // Works fine, but looks weird
    > foo << cout; // -------- the same ---------
    >
    > // cin >> foo; // Of course invalid in for class Foo
    > // cout << foo; // ------------ the same ------------
    >
    > return 0;
    > }
    > // ---------------------------



    I think this problem is because you have not use friend function in this
    class, try add these in your class

    friend ostream &operator>>(ostream &os_o, Foo &ref);
    friend istream &operator<<(istream &is_o, Foo &ref);

    and have these functions defined

    ostream &operator<<(ostream &os_o, Foo &ref)
    {
    os_o << ref.data_;
    return os_o;
    }

    istream &operator>>(istream &is_o, Foo &ref)
    {
    is_o >> ref.data_;
    return is_o;
    }

    hope these can slove your problem, and I think
    cin >> foo;
    cout << foo;
    will work now.

    the problem is, if you define operator>> and operator<< in a class,
    these will be see as operator>>(class typedef, iostream), so if you
    don't wanna use such as foo>>cin nor foo<<cout, you surely shouldn't
    define such like those operator in a class. Instead, you should define
    those by friend function.
    Rayer, Mar 20, 2005
    #3
  4. Alex Vinokur

    Jeff Schwab Guest

    Alex Vinokur wrote:
    > Member operators operator>>() and operator<<() in a program below
    > work fine, but look strange.
    >
    > Is it possible to define member operators operator>>() and operator<<()
    > that work fine and look fine?
    >
    > // --------- foo.cpp ---------
    > #include <iostream>
    > using namespace std;
    >
    > class Foo
    > {
    > private:
    > int data_;
    >
    > public:
    > istream& operator>>(istream& is_o);
    > ostream& operator<<(ostream& os_o);
    >
    > };
    >
    >
    > // -------------
    > istream& Foo::eek:perator>> (istream& is_o)
    > {
    > return is_o >> data_;
    > }
    >
    >
    > // -------------
    >
    > ostream& Foo::eek:perator<<(ostream& os_o)
    > {
    > return os_o << data_ << endl;
    > }
    >
    > // -------------
    > int main()
    > {
    > Foo foo;
    >
    > foo.operator>>(cin);
    > foo.operator<<(cout);
    >
    > foo >> cin; // Works fine, but looks weird
    > foo << cout; // -------- the same ---------
    >
    > // cin >> foo; // Of course invalid in for class Foo
    > // cout << foo; // ------------ the same ------------
    >
    > return 0;
    > }
    > // ---------------------------
    >


    I don't think that looks particularly weird. If it makes you feel
    better, give Foo public set_data( int ) and get_data( ) methods, and use
    non-member operator>> and operator<< to call set_data and get_data.
    Jeff Schwab, Mar 20, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Klatt
    Replies:
    4
    Views:
    466
    Michael Klatt
    Oct 21, 2003
  2. ghager
    Replies:
    6
    Views:
    621
    ghager
    Jan 19, 2006
  3. Alan Woodland

    Template member operators.

    Alan Woodland, Sep 14, 2006, in forum: C++
    Replies:
    3
    Views:
    287
    Victor Bazarov
    Sep 14, 2006
  4. puzzlecracker
    Replies:
    2
    Views:
    269
    Victor Bazarov
    Aug 19, 2007
  5. W Karas
    Replies:
    3
    Views:
    257
    Victor Bazarov
    Nov 30, 2012
Loading...

Share This Page