Why overload << can not be a member function?

Discussion in 'C++' started by dolphin, May 8, 2009.

  1. dolphin

    dolphin Guest

    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?
    dolphin, May 8, 2009
    #1
    1. Advertising

  2. 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::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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 8, 2009
    #2
    1. Advertising

  3. dolphin

    osmium Guest

    "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.
    osmium, May 8, 2009
    #3
  4. dolphin

    Jerry Coffin Guest

    In article <e34c8ac3-7d4e-4283-8811-ca26bb3b3be0
    @j9g2000prh.googlegroups.com>, 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.
    Jerry Coffin, May 8, 2009
    #4
  5. dolphin

    James Kanze Guest

    On May 8, 4:16 pm, 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. 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.

    --
    James Kanze (GABI Software) email:
    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
    James Kanze, May 9, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    362
    Nick Hounsome
    Jan 25, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,747
    Smokey Grindel
    Dec 2, 2006
  3. blangela
    Replies:
    8
    Views:
    647
    Erik Wikström
    Sep 26, 2008
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    236
    Sherm Pendley
    Oct 11, 2004
  5. W Karas
    Replies:
    3
    Views:
    246
    Victor Bazarov
    Nov 30, 2012
Loading...

Share This Page