Why overload << can not be a member function?

D

dolphin

Hi All
Here is my situation:
I have a class named Date, is a class about date and time. I want
to use the cout<<Date. So I want to overload the operator. But why
this operator can not be a member function of this class?
 
V

Victor Bazarov

dolphin said:
Here is my situation:
I have a class named Date, is a class about date and time. I want
to use the cout<<Date.

You can't have a type in the expression like that. It has to be an
object of type 'Date', like

Date d;
cout << d;
> So I want to overload the operator. But why
this operator can not be a member function of this class?

[Assuming you mean that it can't be a non-static member..]

Of which class? Of the 'Date' class? It can. The Date object will
have to be on the left-hand side of the expression, however. You can do
it like this:

class Date {
...
void operator>>(std::eek:stream& os) const {
...
}
};

but then you will be writing

Date d;
d >> cout;

because in an expression 'a @ b' (where '@' is the operator), the
function that defines the operator will *either* be a two-argument
*non*-member, something like

returntype operator @(atype, btype);

or a *member* of 'atype' with a single explicit argument:

returntype atype::eek:perator &(btype);

What book on C++ are you reading that does not explain all that basic stuff?

V
 
O

osmium

dolphin said:
I have a class named Date, is a class about date and time. I want
to use the cout<<Date. So I want to overload the operator. But why
this operator can not be a member function of this class?

Date is not a variable, it is a *type*. C and C++ force you to really
concentrate to keep the two straight in your mind.
 
J

Jerry Coffin

Hi All
Here is my situation:
I have a class named Date, is a class about date and time. I want
to use the cout<<Date. So I want to overload the operator. But why
this operator can not be a member function of this class?

An expression like
x @ y;

where '@' represents some operator that's overloaded as a member
function, is translated as:

x.operator@(y);

This means the overloaded operator has to be a member of the _left_
operand rather than the right operand. In your case, the left operand is
cout instead of your Date type. The compiler will never even look in
your date type for the overloaded operator to satisfy this situation.

The other possibility (the one that works) is to overload the operator
with a free function.
 
J

James Kanze

Here is my situation:
I have a class named Date, is a class about date and time. I want
to use the cout<<Date. So I want to overload the operator. But why
this operator can not be a member function of this class?

The << operator can be, and sometimes is, a member of
std::eek:stream. But you can't add functions to std::eek:stream. And
when a binary operator is overloaded with a member function, the
class it is a member of (the this pointer) is always the left
hand operand. So << for Date can't be a member of std::eek:stream,
because you cannot add functions to std::eek:stream, and it can't
be a member of Date, because this would result in the syntax
Date << cout.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top