Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why overload operator as friend ?

Reply
Thread Tools

why overload operator as friend ?

 
 
Teddy
Guest
Posts: n/a
 
      06-09-2005
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?

 
Reply With Quote
 
 
 
 
Leonhard Vogt
Guest
Posts: n/a
 
      06-09-2005
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
 
Reply With Quote
 
 
 
 
velthuijsen@hotmail.com
Guest
Posts: n/a
 
      06-09-2005
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>&);

 
Reply With Quote
 
velthuijsen@hotmail.com
Guest
Posts: n/a
 
      06-09-2005
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.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      06-09-2005

"Teddy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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




 
Reply With Quote
 
Swampmonster
Guest
Posts: n/a
 
      06-10-2005

> 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:perator 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can we overload '=' operator using friend function c++ Mahain C++ 3 12-20-2007 06:29 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
friend declaration or operator overload function cross namespace. Layton C++ 2 09-27-2006 10:38 PM
friend operator << overload ambiguity error Gianni Mariani C++ 5 01-18-2005 07:11 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM



Advertisments