Click to See Complete Forum and Search --> : << operator overload help


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

HighCommander4
July 24th, 2005, 08:48 PM
Make your operator<< return a reference to the invoking object, for example:

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?

thre3dee
July 24th, 2005, 08:58 PM
Make your operator<< return a reference to the invoking object, for example:

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

NMTop40
July 25th, 2005, 03:48 AM
Normally operator << (when used in a "streaming" context) will take a const reference to the argument, and the >> operator a non-const reference.

humptydumpty
July 25th, 2005, 03:56 AM
For my link list class i'm overloading the << operator to add an object i.e.
mylist << &obj;
but how do you over load it so that i can do it like std::cout i.e
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



friend ostream& operator<<(ostream& os, const IntArray& ia);

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

NMTop40
July 25th, 2005, 05:27 AM
I don't think he wants to stream here, I think he wants to use << as a "chain" to add elements to his list.

SuperKoko
July 25th, 2005, 08:21 AM
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.

RoboTact
July 25th, 2005, 09:50 AM
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.

NMTop40
July 25th, 2005, 11:40 AM
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.