CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2005
    Posts
    94

    << operator overload help

    For my link list class i'm overloading the << operator to add an object i.e.
    Code:
    mylist << &obj;
    but how do you over load it so that i can do it like std::cout i.e
    Code:
    mylist << &obj1 << & obj2 ...;
    and it adds obj1 then obj2 etc etc.
    Anyone know how to do that??

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: << operator overload help

    Make your operator<< return a reference to the invoking object, for example:

    Code:
    class List
    {
    public:
        ...
        List& operator<<(some_type arg)
        {
            ...
             return *this;
        }
    };
    By the way, why would you want the operator<< to take the address of obj1 and obj2 as an argument as opposed to the objects themselves?
    Old Unix programmers never die, they just mv to /dev/null

  3. #3
    Join Date
    Jun 2005
    Posts
    94

    Re: << operator overload help

    Quote Originally Posted by HighCommander4
    Make your operator<< return a reference to the invoking object, for example:

    Code:
    class List
    {
    public:
        ...
        List& operator<<(some_type arg)
        {
            ...
             return *this;
        }
    };
    By the way, why would you want the operator<< to take the address of obj1 and obj2 as an argument as opposed to the objects themselves?
    oh okay i see now. thanks

    EDIT** it takes the address coz thats how my nodes in my link list work
    Last edited by thre3dee; July 24th, 2005 at 09:01 PM.

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: << operator overload help

    Normally operator << (when used in a "streaming" context) will take a const reference to the argument, and the >> operator a non-const reference.

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: << operator overload help

    Quote Originally Posted by thre3dee
    For my link list class i'm overloading the << operator to add an object i.e.
    Code:
    mylist << &obj;
    but how do you over load it so that i can do it like std::cout i.e
    Code:
    mylist << &obj1 << & obj2 ...;
    and it adds obj1 then obj2 etc etc.
    Anyone know how to do that??
    general method is here is decleration and defination

    Code:
    friend ostream&    operator<<(ostream& os, const IntArray& ia);
    
    ostream& 
    operator<<(ostream& os, const IntArray& ia) 
    {
          //do something
    }
      os << endl;
      return os;
    }

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: << operator overload help

    I don't think he wants to stream here, I think he wants to use << as a "chain" to add elements to his list.

  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: << operator overload help

    Quote Originally Posted by thre3dee
    oh okay i see now. thanks

    EDIT** it takes the address coz thats how my nodes in my link list work
    You can take the address of a reference.

    But, maybe you don't handle correctly the lifetime of objects.
    Maybe you need to copy objects and allocate them on the heap with new.

    You must be sure to don't use the address of objects after they have been destroyed (local objects are destroyed when the function is exited, and heap objects are destroyed when delete or delete[] is called.

  8. #8
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: << operator overload help

    Abuse of operator overloading isn't good. Maybe it would be better to just use function and not write such sequences. Standard use of operator<< is stream output. Though if list can contain different types of elements it may be good.
    Last edited by RoboTact; July 25th, 2005 at 09:58 AM.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: << operator overload help

    Standard use of << was originally a bit-shift operator.

    But with regards to the use of << by streams, it suggests pushing in multiple objects, possibly of different types, as an ordered sequence, so using it to add things to a list is not an unreasonable use. However there is already a collection std::list and no particular reason to write your own.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured