overloading <<

This is a discussion on overloading << within the C++ Programming forums, part of the General Programming Boards category; Can we chain an overloaded << operator cout <<a<<b without declaring the overloaded operator<< function friend? I know that if ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    overloading <<

    Can we chain an overloaded << operator

    cout <<a<<b

    without declaring the overloaded operator<< function friend?

    I know that if there is no access to private members then we can avoid the friend declaration but how is it linked to << operator chaining?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    As long as it returns the output stream it can. But if you make it a member, you would not be able to use it in the way you described.

    It chains because initially it receives both the object it's associated with and an output stream. When it returns the output stream it can then be passed on to the next operator.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Can we chain an overloaded << operator

    cout <<a<<b

    without declaring the overloaded operator<< function friend?
    Yes. For example, there might be a print() member function, thus operator<< can be implemented in terms of the print() member function and so would not need to be a friend.

    how is it linked to << operator chaining?
    It is not linked to operator chaining. The operator chaining comes from returning a reference to the ostream that was passed as the first argument to overloaded operator<<.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This operator is usually overloaded for std::ostream like that:

    Code:
    std::ostream& operator << (std::ostream& os, const UserType& sth);
    The returned reference (to cout in your case) is passed as the left argument to the next operator call in the chain.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is two questions here:
    1. How to chain a operator<< function to another operator<< function?
    That is quite easy: Just make the return type a "ofstream &".

    2. How do I make a operator<< for some particular type?
    Two obvious choices: either make it a friend function, or make it part of the class itself (in which case it takes only te ofstream parameter as input).
    There are non-obvious choices too:
    - Make an operator function to convert your class into a type that can be output using a standard operator<< function. Typically, int or string would work.
    - Make the member data public.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    or make it part of the class itself (in which case it takes only te ofstream parameter as input).
    How does that work? I had the impression that this would result in the current object of that class being on the left hand side of the expression, but the overloaded operator<< for std::ostream requires a std::ostream to be passed on the left hand side of the expression.

    EDIT:
    Or... are you interpreting the question in a more general fashion despite the canonical "overload operator<< for ostream" example?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    How does that work? I had the impression that this would result in the current object of that class being on the left hand side of the expression, but the overloaded operator<< for std::ostream requires a std::ostream to be passed on the left hand side of the expression.

    EDIT:
    Or... are you interpreting the question in a more general fashion despite the canonical "overload operator<< for ostream" example?
    Nah, I just messed up, didn't I :-(

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Yes. For example, there might be a print() member function, thus operator<< can be implemented in terms of the print() member function and so would not need to be a friend.
    can you elaborate some more on this maybe with some code?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    An example might be:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        X() : member(0) {}
        X(int m) : member(m) {}
        void print(std::ostream& out) const
        {
            out << member;
        }
    private:
        int member;
    };
    
    std::ostream& operator<<(std::ostream& out, const X& x)
    {
        x.print(out);
        return out;
    }
    
    int main()
    {
        X x1;
        std::cout << x1 << std::endl;
        X x2(123);
        std::cout << x2 << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    There's nothing wrong with making the operator<< a friend if that's necessary. Sometimes it's not necessary because public interface functions provide enough information to do the work.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nevertheless, the default position for any functionality should be that it is best to make it a non-friend, non-member function. If that isn't possible because of language rules (operator = and similar things) or because it needs access to class internals, make it a member function. If that isn't possible because it's an operator whose first argument isn't of the class type, make it a friend.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In one case, I made use of the fact that I had a "toString" function in the class, and just used that inside the operator<< function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading <<
    By msshapira in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2009, 03:11 PM
  2. Overloading << and >>
    By MMu in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2008, 07:49 AM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:17 AM
  4. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 05:33 PM
  5. Overloading the << operator.
    By antex in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2005, 02:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21