|
-
July 24th, 2005, 08:44 PM
#1
<< operator overload help
For my link list class i'm overloading the << operator to add an object i.e.
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??
-
July 24th, 2005, 08:48 PM
#2
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
-
July 24th, 2005, 08:58 PM
#3
Re: << operator overload help
 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.
-
July 25th, 2005, 03:48 AM
#4
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.
-
July 25th, 2005, 03:56 AM
#5
Re: << operator overload help
 Originally Posted by thre3dee
For my link list class i'm overloading the << operator to add an object i.e.
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;
}
-
July 25th, 2005, 05:27 AM
#6
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.
-
July 25th, 2005, 08:21 AM
#7
Re: << operator overload help
 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.
-
July 25th, 2005, 09:50 AM
#8
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."
-
July 25th, 2005, 11:40 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|