473,508 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strangely operator> overload

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::operator<(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(){}
---------------------------------------------------------------
i need the virtual function and then inherit its class, overload
operator< (not virtual) and use polimorphism call function.

Apr 3 '07 #1
8 1946
devphylosoff wrote:
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::operator<(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 3 '07 #2
devphylosoff <de*****@gmail.comwrote:
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::operator<(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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 3 '07 #3
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::operator>(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 ? :>

Apr 3 '07 #4
devphylosoff wrote:
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.
You gave the lecture? You listened to the lecture?

You still didn't answer what book you were reading. None?
there was:
------
class Integer{
virtual bool operator>(const Integer& a, const Integer& b) = 0;
That's not C++. A member operatorshall have only one explicit
argument.
};
------

moreover, someone wrote:

In C++, these two functions are equivalent:

bool Pair::operator>(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 3 '07 #5
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 operatorshall have only one explicit
argument.
rigth

In C++, these two functions are equivalent:
bool Pair::operator>(const Pair&) // C++ style
bool operator>(const Pair&, const Pair&) // C style

Nothing "C" in the latter. Where did you see this comment?
group: utexas.class.cs105.c++ -"< overloading"
http://groups.google.pl/group/utexas...82b1d05579b175

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

Apr 3 '07 #6
devphylosoff wrote:
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
Apr 3 '07 #7
On Apr 3, 11:49 pm, Fei Liu <fei...@aepnetworks.comwrote:
devphylosoff wrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Apr 4 '07 #8
James Kanze wrote:
On Apr 3, 11:49 pm, Fei Liu <fei...@aepnetworks.comwrote:
>devphylosoff wrote:
>>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.
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
Apr 4 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3785
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
2464
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
7
4659
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
7
1570
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will...
9
2367
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
6
2073
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor...
1
1590
by: semut | last post by:
Hi, just like to find out when will this operator overload function gets triggered? Code Snippet class A { public:
11
2041
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
14
2783
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.