472,804 Members | 1,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,804 software developers and data experts.

why overload operator as friend ?

Hello all

consider the class Date declaretion below:

class Date {
public:
Date();
Date(int year, int month, int day);
Date(const string&);
int getYear() const;
int getMonth() const;
int getDay() const;

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

private:
int years;
int months;
int days;
};

Can I remove the keyword friend in the six overload operator
declaretions?
If I did so, would they still act the same?

Jul 23 '05 #1
5 2064
Teddy wrote:
Hello all

consider the class Date declaretion below:

class Date {
public: [...] int getYear() const;
int getMonth() const;
int getDay() const;

friend bool operator<(const Date&, const Date&);
friend bool operator>(const Date&, const Date&); [...] private:
int years;
int months;
int days;
};

Can I remove the keyword friend in the six overload operator
declaretions?
Yes you can, but then there is no need (or not even legal?) to place
them inside the class.
If I did so, would they still act the same?


Depends on how you have implemented them, if you don't declare them
as a friend you will not be able to access the private data members
directly, but only via the public access functions.

Leonhard
Jul 23 '05 #2
Teddy wrote:
Hello all

consider the class Date declaretion below:

class Date {
public:
Date();
Date(int year, int month, int day);
Date(const string&);
int getYear() const;
int getMonth() const;
int getDay() const;

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

private:
int years;
int months;
int days;
};

Can I remove the keyword friend in the six overload operator
declaretions? No. You'd get a complaint that you cannot have a binary version of the
operator at this point in the file.
If you'd alter the entire thing to:
bool operator<op type>(const Date&) const;
and alter the function body to work within the class then it would work
again.
If I did so, would they still act the same?

Yes. If done correctly the user should not be able to see any
difference in you switching from the binary version to the unary (in
this case).

You should be able to go to the unary versions of the operators seeing
that you have left and right as the same class.
Generally binary versions are used for things like:
friend operator<op type>(const <type1>&, const <type2>&);
friend operator<op type>(const <type2>&, const <type1>&);

Jul 23 '05 #3
I need to think out these replies a bit more.

operator<op type>(const <class>&) const;
is still binary (it gets a hidden *this added to the function, which is
also the reason why
operator<op type>(const <class>&, const <class>&);
complains, it expects 2 input vars and gets 3

Why doesn't this happen if you do
friend operator<op type>(const <class>&, const <class>&);
Because you tell your class that a function with this prototype has
full access
to the non public parts of your class.

Jul 23 '05 #4

"Teddy" <du********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hello all

consider the class Date declaretion below:

class Date {
public:
Date();
Date(int year, int month, int day);
Date(const string&);
int getYear() const;
int getMonth() const;
int getDay() const;

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

private:
int years;
int months;
int days;
};

Can I remove the keyword friend in the six overload operator
declaretions?
If I did so, would they still act the same?


I see that you have public accessor functions for the private members. So,
if you remove those friend declarations entirely and just define the
functions globally (outside the class), then they can use the accessor
functions (e.g., getYear) to make their comparisons.

But if you simply remove the friend specifier, then suddenly those become
member function declarations, and you need to modify the function
definitions and parameter lists accordingly.

-Howard


Jul 23 '05 #5
I need to think out these replies a bit more.

operator<op type>(const <class>&) const;
is still binary (it gets a hidden *this added to the function, which is
also the reason why
operator<op type>(const <class>&, const <class>&);
complains, it expects 2 input vars and gets 3

Why doesn't this happen if you do
friend operator<op type>(const <class>&, const <class>&);
Because you tell your class that a function with this prototype has
full access
to the non public parts of your class.


if you do:

class foo {
...
return_type operator op_type (const foo&, const foo&);

you tell the compiler that there is a _member_ function "operator<op
type>" that returns a "return_type" and accepts an implicit "this"
pointer and 2 explicit arguments of type "const foo&". therefor the
compiler will complain because you're declaring a binary operator that
get's passed 3 arguments (1 implicit "this", 2 explicit).

if you do:

class foo {
...
_friend_ return_type operator op_type (const foo&, const foo&);

you tell the compiler that the _free_ function "return_type operator
op_type(..." is a friend of class "foo".
i.e. your allow it access to all protected and private members of "foo".
and since _free_ functions are not members of any class they have no
implicit "this" pointer, and hence the 2 arguments are ok (except for
unary operators of course).

--

in the first case (after you remove the 2nd "const foo&" argument) you'd
have to define the member function like this:

return_type foo::operator op_type (const foo& right)
{
// compare "*this" vs. "right"
...

i.e. qualify the name with "foo::" since the function is a member of
foo. in the second case you don't have to qualify the name (since it's a
_free_ function) and only write:

return_type operator op_type (const foo& left, const foo& right)
{
// compare "left" vs. "right"
...

and finally, if you only need "public" access in your _free_ operator
function, then you can simply delete the whole friend declaration from
the "foo" class - but you need to delete the whole line, not just the
"friend" keyword. if you just delete the "friend" keyword you will
change a "friend declaration" into a "member function declaration" which
are 2 completely different things. they look similar, but they are not.
Jul 23 '05 #6

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

Similar topics

7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
5
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...
5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
13
by: Atlas | last post by:
Hi, I implemented a template as: template <int L, int M, int T> class Quantity { ..... public: friend Quantity operator*(const Quantity& q1,const Quantity& q2); ..... };
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
14
by: Rg | last post by:
Hello, everyone. I'm curious. Why can't I overload an operator only with pointers to class objects? For instance: class SomeClass { friend SomeClass *operator<<(SomeClass *, int); };
8
by: devphylosoff | last post by:
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...
1
by: =?utf-8?B?5Zyw55CD5Y+R5Yqo5py6?= | last post by:
In C++ we can not overload the explicit typecast operator like static_cast, dynamic_cast, const_cast and reinterpret_cast. The only we can do is the implicit typecast operator like operator...
11
by: onkar | last post by:
#include<iostream> using namespace std; class Integer{ int i; public: Integer(int ii):i(ii){} const Integer operator+(const Integer& rv){ cout<<"1-operator+"<<endl; return Integer(i+rv.i); }
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.