operator definition

L

lou zion

hello all,

i'm just trying to get my syntax correct for defining the operators for a
class.

i have class 'Date' and i want to define

bool operator!=(const Date &,const Date &);
bool operator< (const Date &,const Date &);

i've tried lots of variations on this but can't seem to crack the 'code'.

what's the proper syntax? thnx!

lou
 
A

Andre Kostur

hello all,

i'm just trying to get my syntax correct for defining the operators
for a class.

i have class 'Date' and i want to define

bool operator!=(const Date &,const Date &);
bool operator< (const Date &,const Date &);

i've tried lots of variations on this but can't seem to crack the
'code'.

what's the proper syntax? thnx!

Uh, you don't mention what the problem actually is. Also, are you trying
to declare those as members of the Date class, or free-standing functions?
 
M

Matthias Kaeppler

Andre said:
Uh, you don't mention what the problem actually is. Also, are you trying
to declare those as members of the Date class, or free-standing functions?

Because, if they are members of your class, 'this' is implicitly always
the left hand side and you must not provide two parameters, but only one
(the right hand side).
 
E

E. Robert Tisdale

lou said:
I'm just trying to get my syntax correct
for defining the operators for a class.

I have class 'Date'

class Date {
bool operator!=(const Date&) const;
bool operator< (const Date&) const;
};
and I want to define
bool operator!=(const Date&, const Date&);
bool operator< (const Date&, const Date&);

I've tried lots of variations on this but can't seem to crack the 'code'.

What's the proper syntax?

You could also define non-member [friend] functions:

class Date {
friend
bool operator!=(const Date&, const Date&);
friend
bool operator< (const Date&, const Date&);
};
 
W

wittempj

It is my practice to declare operators as friends, like below

-#include <iostream>
-using namespace std;
-
-class Date
-{
- int m_year, m_month, m_day;
-public:
- Date(int year, int month, int day):m_year(year), m_month(month),
m_day(day) {}
- friend bool operator!=(const Date& l, const Date& r);
-};
-
-bool operator!=(const Date& l, const Date& r)
-{
- if (l.m_year != r.m_year) return true;
- if (l.m_month != r.m_month) return true;
- if (l.m_day != r.m_day) return true;
- return false;
-}
-
-
-int main()
-{
- Date d(2005, 4, 5), e(2005, 4, 4), f(2005, 4, 4);
- (d != e) ? cout << "unequal": cout << "equal";
- cout << endl;
- (e != f) ? cout << "unequal": cout << "equal";
- cout << endl;
-
- return 0;
-}
 
L

lou zion

Matthias Kaeppler said:
Because, if they are members of your class, 'this' is implicitly always
the left hand side and you must not provide two parameters, but only one
(the right hand side).

well, actually, i was just trying to do free-standing functions, but i'd
like to do it as part of the class as well. how do you even use operator<
with an implied 'this'?

my underlying problem is that the c++ sort algorithm uses the '<' operator
to do its sorting and i was hoping i could specify which variable of the
class it uses for the compare. in this class, i could sort by full date,
month only, day only, year only, julian date, or some other attribute. is
this possible?

thanks again!

lou
 
P

Peter Koch Larsen

lou zion said:
hello all,

i'm just trying to get my syntax correct for defining the operators for a
class.

i have class 'Date' and i want to define

bool operator!=(const Date &,const Date &);
bool operator< (const Date &,const Date &);

i've tried lots of variations on this but can't seem to crack the 'code'.

what's the proper syntax? thnx!

lou
I do not see the problem. The definition looks just perfect, assuming this
is a free-standing function (as it should be). If you have to, perhaps you
need to make the function a friend.

/Peter
 
K

Karl Heinz Buchegger

lou said:
well, actually, i was just trying to do free-standing functions, but i'd
like to do it as part of the class as well. how do you even use operator<
with an implied 'this'?

Like any other member function. You just access the variables.
my underlying problem is that the c++ sort algorithm uses the '<' operator
to do its sorting and i was hoping i could specify which variable of the
class it uses for the compare. in this class, i could sort by full date,
month only, day only, year only, julian date, or some other attribute. is
this possible?

Sure it is possible.
Why don't you show us the code you have (even if it doesn't compile).
This way we would have something to work with and can show you where
your thinking is flawed.

On the other hand: If you want to select at runtime what member variable
should be selected for the comparison, then op< is not a good choice.
The C++ sort algorithm takes an optional argument, the name of
a function or a functor object, which can be used exactly for that:
Free the algorithm from using op< for the comparison but using that
function (or functor) instead.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top