strangely operator> overload

D

devphylosoff

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);
or
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);
};

main(){}
 
V

Victor Bazarov

devphylosoff said:
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);
or
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<" ?

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?

V
 
M

Marcus Kwok

devphylosoff said:
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);
or
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?

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

devphylosoff

ok, for one argument all is fine:
for 'friend', 'virtual' and normal function definig
What book are you reading that doesn't explain member vs. non-member
operator overloading?

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

moreover:
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 ? :>
 
V

Victor Bazarov

devphylosoff said:
ok, for one argument all is fine:
for 'friend', 'virtual' and normal function definig


i try to execute example from my Ph.d lecture.

You gave the lecture? You listened to the lecture?

You still didn't answer what book you were reading. None?
there was:

That's not C++. A member operator> shall have only one explicit
argument.
};
------

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

Nothing "C" in the latter. Where did you see this comment?
In the first one, the left argument gets passed implicitly as the
object (this), and in the second one both arguments are passed
explicitly.

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

OK, any of those actually member functions?
what's wrong in my mind ? :>

I don't know the answer to this very generic question.

V
 
D

devphylosoff

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;
};

That's not C++. A member operator> shall have only one explicit
argument.
rigth




Nothing "C" in the latter. Where did you see this comment?

group: utexas.class.cs105.c++ -> "< overloading"
http://groups.google.pl/group/utexa...ument"+operator&rnum=1&hl=pl#b682b1d05579b175

OK, any of those actually member functions?

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

Fei Liu

devphylosoff said:
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;
};

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
 
J

James Kanze

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.

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

Fei Liu

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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top