can I define this function as a member function?

closed account (iw0XoG1T)
1
2
3
4
ostream& operator<<(ostream& os, Input_win& w)
{
    return os << w.value();
}


I would like to define the above function within the class Input_win. Can it be done?

edit: never mind it just occurred to me that this function would have not practical purpose.
Last edited on
I'm pretty sure that you couldn't possibly define it as a member because then you'd have to type
object << cout;
rather than
cout << object;
closed account (iw0XoG1T)
@tummychow

I wish I understood your response because I always find overloading operators difficult. What I find myself doing is copying the code and not understanding why it works.
Well the left operand of a member operator must always be an object of the class that contains it.
So since cout is the left operand of <<, you would have to have it as a member of iostream (or specifically, ostream) to make it work the way it's normally used. So it has to stand alone.
closed account (iw0XoG1T)
Do all operators have a left operand and a right operand? Also is the first parameter always the left operand?
All binary operators (with two operands) have two operands. And the first parameter is always the left, in a standalone. If it's a member, the left is the class it's a member of, and it only takes one operand.
closed account (iw0XoG1T)
Thank you, but just to make sure I have some understanding; the << operator would be considered a standalone(correct), and + operator would be considered a binary operator?
+ is a binary operator. But it could be treated as member or nonmember.
Both << and + are binary operators, because they have two things (cout and an object, or two objects). An operator such as increment, ++, would be unary. There is only one ternary operator, the ? operator, and I don't remember if you can overload that.
closed account (iw0XoG1T)
Got it--and I just read an explanation on wikipedia. Thank you, so much I am not sure why and had so much trouble wrapping my mind around this concept.
Topic archived. No new replies allowed.