By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,378 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

operator definition

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"lou zion" <il********@adelphia.net> wrote in
news:Ro********************@adelphia.com:
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?
Jul 23 '05 #2

P: n/a
Andre Kostur wrote:
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).

--
Matthias Kaeppler
Jul 23 '05 #3

P: n/a
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;
-}

Jul 23 '05 #4

P: n/a
lou zion wrote:
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&);
};
Jul 23 '05 #5

P: n/a

"Matthias Kaeppler" <no****@digitalraid.com> wrote in message
news:d3*************@news.t-online.com...
Andre Kostur wrote:
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).

--
Matthias Kaeppler


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
Jul 23 '05 #6

P: n/a

"lou zion" <il********@adelphia.net> skrev i en meddelelse
news:Ro********************@adelphia.com...
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
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.