Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why overload << can not be a member function?

Reply
Thread Tools

Why overload << can not be a member function?

 
 
dolphin
Guest
Posts: n/a
 
      05-08-2009
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-08-2009
dolphin wrote:
> 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: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:perator &(btype);

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      05-08-2009
"dolphin" wrote:

> 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.


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      05-08-2009
In article <e34c8ac3-7d4e-4283-8811-ca26bb3b3be0
@j9g2000prh.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-09-2009
On May 8, 4:16 pm, dolphin <(E-Mail Removed)> wrote:

> 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:stream. But you can't add functions to std: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:stream,
because you cannot add functions to std:stream, and it can't
be a member of Date, because this would result in the syntax
Date << cout.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Member vs. non-member operator overload disambiguation W Karas C++ 3 11-30-2012 06:34 PM
why const member, reference member can only be initialized not assigned ww C++ 4 10-26-2007 11:22 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments