strangely operator> overload

Discussion in 'C++' started by devphylosoff, Apr 3, 2007.

  1. devphylosoff

    devphylosoff Guest

    I can write
    friend bool operator<(const Integer& left, const Integer& right);

    but i cannot use
    virtual bool operator<(const Integer& left, const Integer& right);
    bool operator<(const Integer& left, const Integer& right);

    no.cpp:10: error: bool Integer::eek:perator<(const Integer&, const
    Integer&) must take exactly one argument

    1. why this error does not occur while use "friend" prefix?
    2. and how to create "virtual bool operator<" ?

    in code :
    #include <iostream>
    using namespace std;

    class Integer{
    friend bool
    operator<(const Integer& left,
    const Integer& right);
    virtual bool
    operator>(const Integer& left,
    const Integer& right);

    devphylosoff, Apr 3, 2007
    1. Advertisements

  2. A member operator < (virtual or not) has to have only one explicit
    argument (the other, implicit, is the object for which it's called).

    What book are you reading that doesn't explain member vs. non-member
    operator overloading?
    Victor Bazarov, Apr 3, 2007
    1. Advertisements

  3. devphylosoff

    Marcus Kwok Guest

    When you use "friend", then that means that you are declaring an
    external (i.e., not a member of the class) function that will have
    access to the protected and private parts of your Integer class.

    When you do not use "friend", then it declares the operator as a member
    of the class. So, for example, if it *is* a member, then given:

    Integer i1, i2;
    bool b = i1 < i2;

    is equivalent to

    Integer i1, i2;
    bool b = i1.operator<(i2);

    This explains the message about the operator only taking one argument.
    The way you declared it, it is expecting something equivalent to:

    Integer i1, i2, i3;
    bool b = i1.operator<(i2, i3);

    which does not make sense.
    Marcus Kwok, Apr 3, 2007
  4. devphylosoff

    devphylosoff Guest

    ok, for one argument all is fine:
    for 'friend', 'virtual' and normal function definig
    i try to execute example from my Ph.d lecture.

    there was:
    class Integer{
    virtual bool operator>(const Integer& a, const Integer& b) = 0;

    moreover, someone wrote:

    In C++, these two functions are equivalent:

    bool Pair::eek:perator>(const Pair&) // C++ style
    bool operator>(const Pair&, const Pair&) // C style

    In the first one, the left argument gets passed implicitly as the
    object (this), and in the second one both arguments are passed

    you can find many examples in google/codesearch
    for "bool operator<(const Class & ref1, const Class $ ref2) - with 2
    arguments !

    what's wrong in my mind ? :>
    devphylosoff, Apr 3, 2007
  5. You gave the lecture? You listened to the lecture?

    You still didn't answer what book you were reading. None?
    That's not C++. A member operator> shall have only one explicit
    Nothing "C" in the latter. Where did you see this comment?
    That's true.
    OK, any of those actually member functions?
    I don't know the answer to this very generic question.

    Victor Bazarov, Apr 3, 2007
  6. devphylosoff

    devphylosoff Guest

    the code from book(sorry - i dont write what the book is) is exactly
    as below:

    class GreaterThanComparable {
    virtual bool operator>(const GreaterThanComparable &a, const
    GreaterThanComparable &b) = 0;

    group: utexas.class.cs105.c++ -> "< overloading""+operator&rnum=1&hl=pl#b682b1d05579b175

    i don't think so, but my teacher had in code as a member function (i
    paste at the top).
    i will ask him tommorow.
    devphylosoff, Apr 3, 2007
  7. devphylosoff

    Fei Liu Guest

    Just because it's from a book, it does not make it correct. When used as
    a member function, operator > can have exactly one argument. But it's
    preferred that it's implemented as a non-member function to be expanded
    in generic ways.

    Fei Liu, Apr 3, 2007
  8. devphylosoff

    James Kanze Guest

    Not so much for it to be expanded in generic ways (I'm not even
    sure what that is supposed to mean), but rather so that the same
    conversion rules apply to both operands. If there are no
    conversions involving your type, there's no reason not to make
    it a member.

    With regards to how to solve the poster's original problem: I
    usually provide a member function compare, which returns an
    integer <, == or > 0, according to whether *this is <, == or >
    the other object, and then implement all of the comparison
    operators using it. (In fact, I generally use the
    Barton-Nackman trick, and derive from a templated base class
    which has the implementations, defined as inline friend
    functions.) And of course, compare can easily be virtual
    (although defining the semantics for such a case could be a bit
    tricky, since you will end up comparing Apples to Oranges, if
    both derive from Fruits).

    Actually, with regards to the poster's original problem, if his
    prof presents code like that he quoted, I would suggest changing
    James Kanze, Apr 4, 2007
  9. devphylosoff

    Fei Liu Guest

    I am referring to examples such as std::swap. It's usually easier to use
    template with a standalone function and make such APIs available to users.

    Fei Liu, Apr 4, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.